Sunday, January 27, 2013

Create an approval workflow in SharePoint Designer 2010

This is a video tutorial that explains about how to create an approval workflow in SharePoint designer 2010.

You can also check SharePoint 2013 workflow development tools and Create a sequential workflow using Visual studio 2010 in SharePoint 2010 [Read here].

Thursday, January 24, 2013

Check if Item is in document set in SharePoint 2010

In this post we will discuss how to check if an item is present in a document set programmatically using SharePoint 2010 object model.

You can also check my previous posts on: WebPart custom Properties, Download CAML designer for SharePoint 2013 and What you can do with Sandboxed Solutions?

To work with document sets using SharePoint 2010 object model, we have to use

Microsoft.Office.DocumentManagement.DocumentSets namespace.

Below is the full code:

public bool IsDocumentSetItem(SPListItem itemToCheck)
{
bool documentSetItem = false;

if (itemToCheck.File != null)
{

DocumentSet documentSet = DocumentSet.GetDocumentSet(itemToCheck.File.ParentFolder);

if (null != documentSet)
{
documentSetItem = true;
}
}
return documentSetItem;
}

Tuesday, January 22, 2013

Upload file to document library using SharePoint 2010 obect model

In this article we will discuss about how to upload a file to a document library programmatically using SharePoint 2010 object model.

Here we will upload a document to the Shared Documents document library.

You can also check out my previous posts on: RunWithElevatedPrivileges in SharePoint 2010, Redirect User to Success or Error page in SharePoint and New Features of SharePoint 2013.

using (SPSite site = new SPSite("http://SiteURL"))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;

SPFolder folder = web.Folders[site.URL + "/Shared Documents/"];

byte[] content = null;

using (FileStream filestream = new FileStream(@"C:\MyDocument.docx",System.IO.FileMode.Open))
{
content = new byte[(int) filestream.Length];

filestream.Read(content, 0, (int) filestream.Length);

filestream.Close();
}

SPFile file = folder.Files.Add("MyDocument.docx", content, true);

}
}

Wednesday, January 16, 2013

Add an attachment to List item using SharePoint object model

In this post we will discuss about how to add an attachment using SharePoint 2010 object model.

You can also check my previous posts on Get 14 hive file path by using SharePoint 2010 object model [Read here], Get URL value SharePoint hyperlink field [Check here] and How to get Absolute URL of List using SharePoint object model? [Solution here].

string listItemID = "ID of the list item";

using (SPSite site = new SPSite("http://Site URL"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["MyCustomList"];
SPListItem newItem = list.GetItemById(listItemID);

byte[] contents = null;

if (fileUpload1.PostedFile != null && fileUpload1.HasFile)
{

using (Stream fileStream = fileUpload1.PostedFile.InputStream)
{
contents = new byte[fileStream.Length];
fileStream.Read(contents, 0, (int) fileStream.Length);
fileStream.Close();
}

SPAttachmentCollection attachments = newItem.Attachments;
string fileName = Path.GetFileName(fileUpload1.PostedFile.FileName);
attachments.Add(fileName, contents);
newItem ["AttachmentName"] = fileName;
newItem.Update();

}
}
}

Here fileUpload1 is the file uploaded control. And AttachmentName is the column name in the list.

Tuesday, January 15, 2013

Create list programmatically using SharePoint object model

In this post we will discuss about how to create a SharePoint list using object model.

You can also check my previous posts on How to Create List and Add Item to that List using SharePoint 2010 client object model? [Solution here], RunWithElevatedPrivileges in SharePoint 2010 [Read here] and Different ways to check logs in SharePoint [Check here].

Below is the code to create list programmatically using SharePoint object model.

using (SPSite site = new SPSite("http://Site URL"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = null;
string listName = "MyCustomList";

// Check whether the list already exists
try
{
list = web.Lists[listName];
}
catch (Exception ex)
{
}

if (list == null)
{
Guid listId = web.Lists.Add(listName, "Our Custom List", SPListTemplateType.GenericList);
list = web.Lists[listId];
list.OnQuickLaunch = true;
list.Update();
}
}
}

The above code will create a generic list without any extra field. And it will show in Quick Launch.

You can also add custom columns like below:

list.Fields.Add("FirstName", SPFieldType.Text, true);
list.Update();

Saturday, January 12, 2013

Create and Add user to Groups in SharePoint using Object model

In this post we will discuss how to create a SharePoint group and how to add users to the particular group using SharePoint object model.

You can also check my previous posts on ECMA Client Object Model (SP.js) in SharePoint 2010 [Read here], Client Object Model SharePoint 2010 [Read here], Create list using object model in SharePoint [Read here].

Below is the full code:

SPSite site = new SPSite("http://Site URL");

using (SPWeb web = site.OpenWeb())
{
string newGroup = "MyGroup";

web.SiteGroups.Add(newGroup, web.CurrentUser, web.CurrentUser,"This is our new group");

SPGroup group = web.SiteGroups[newGroup];

SPRoleAssignment roles = new SPRoleAssignment(group);

SPRoleDefinition permission = web.RoleDefinitions["Full Control"]; //Here the group will have Full Control permission level.

roles.RoleDefinitionBindings.Add(permission);

web.RoleAssignments.Add(roles);

// Add users to this group
SPUser newUser = web.AllUsers[@"Administratoe\Fewlines4Biju"];

group.AddUser(newUser);
}

The above code will create a group and add a new user to the group.

Thursday, January 10, 2013

How to get Absolute URL of List using SharePoint object model?

In this post we will discuss how to get absoulte URL of a list using SharePoint object model.

You can also check my previous posts on Get 14 hive file path by using SharePoint 2010 object model [Solution here], Add safe controls programmatically in SharePoint 2010 [Solution here] and Add items to SharePoint list using PowerShell [Solution here].

We can use the SPUtility class to retrieve the absolute or full URL of the SharePoint list.

Wednesday, January 9, 2013

Delete SharePoint List using PowerShell

In this post we will discuss how we can delete a SharePoint list using PowerShell.

You can also check my PowerShell articles on Get System Accounts using PowerShell, Backup and Restore site collection using PowerShell, Switch from classic to claims authentication in SharePoint 2010.

Below is the code to delete a SharePoint list using PowerShell.

Tuesday, January 8, 2013

Programmatically set a user as the Site Collection Administrator in SharePoint

In this post we will discuss about how to set a user as the Site Collection Administrator in SharePoint programmatically.

You can also check my previous articles on ECMA Client Object Model (SP.js) in SharePoint 2010, Get URL value SharePoint hyperlink field and Client Object Model SharePoint 2010.

string siteUrl = "http://Site URL";
string strUserName = "DomainName/UserName";

Thursday, January 3, 2013

Benifits of SharePoint designer

In this post we will discuss some of the benifits of SharePoint designer.

You can check my previous posts on Create views using SharePoint designer 2010, Create list using SharePoint Designer 2010.

SharePoint designer is a very powerful tool for developers and power users. There are some features of SharePoint designer 2010 like below:

- Administrators can decide per site collection what exactly a power user operating SharePoint Designer is permitted to do. Check this article for more information on this.

- The new ribbon-based Office 2010 environment is now context sensitive. Means the option will come according to the tab you chose rather showing all options.

- It’s easy to create pages, pagelayouts, lists, and workflows etc.

- It’s easy to create content types and use them immediately for a list or library.

- In assets library you can store and deploy resources such as images, stylesheets, and support files.

- You can get the list views as XSLT and modify the views by editing the underlying XSLT.

- You can get access to external data sources such as web services, databases, XML files, and scripts.

- You can easily create BCS data sources, modify then and use them.

- You can create and modify list workflows, site workflows, and reusable workflows.

- You do not need vast knowledge to work with SharePoint designer.

You can check how To Hide Ribbon From Anonymous Users in Sharepoint 2010 here.

Modify SharePoint list using PowerShell

In this post we will discuss how we can modify SharePoint list using PowerShell.

You can also check my previous posts on how to add items to SharePoint list using PowerShell , PowerShell commands for SharePoint database and Powershell script to get SharePoint feature IDs.

Below is the PowerShell command to update a SharePoint List.

$site    =     new-object Microsoft.SharePoint.SPSite("http://site URL")

Tuesday, January 1, 2013

Which SharePoint 2010 objects should not be disposed

In this post we will discuss about what are the SharePoint objects which should not be disposed explicitly.

You can also check my previous posts on Detecting Memory Leaks in SharePoint 2010, Disposing SharePoint Objects and Client Object Model SharePoint 2010.

Below are some objects which should not disposed:

SPSite site = SPContext.Current.Site;

SPWeb rootWeb = site.RootWeb;

SPWeb web = SPContext.Web;

SPWeb web = SPContext.Current.Web;

SPSite site = SPContext.Site;

SPSite site = SPContext.Current.Site;

Next we should not dispose objects used in a feature receiver like:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
}

The below objects should not be disposed.

SPWebEventProperties.Web

SPListEventProperties.Web

SPListEventProperties.List.Web

SPItemEventProperties.ListItem.Web

- Those objects used with personal pages:

The Microsoft.SharePoint.Portal.WebControls.IPersonalPage interface has two properties that return SPSite and SPWeb objects, respectively: IPersonalPage.PersonalSite and IPersonalPage.PersonalWeb.