this blog contains information for .net and sql stuffs. You can find various tips and tricks to overcome problem you may be facing in ...

Wednesday, February 22, 2012

Programmatically create folder in SharePoint Document library with client object model

SharePoint 2010 has very strong client object library, with the help of client object model we can almost interact with all document related functionality from our application and can interact with SharePoint 2010.

Below is some code snippet for creating new folder in the SharePoint Document Library.

public bool CreateSPFolder(string libname, string correspondance)


bool bolReturn = false;

ClientContext ctx = this.GetSPClientContext();

Web web = ctx.Web;

List documentLib = ctx.Web.Lists.GetByTitle(libname);

string targetFolderUrl = libname + "/" + correspondance;

Folder folder = web.GetFolderByServerRelativeUrl(targetFolderUrl);


bool exists = false;




exists = true;


catch (Exception ex)


bolReturn = false;


if (!exists)


ContentTypeCollection listContentTypes = documentLib.ContentTypes;

ctx.Load(listContentTypes, types => types.Include

(type => type.Id, type => type.Name,

type => type.Parent));

var result = ctx.LoadQuery(listContentTypes.Where

(c => c.Name == "Folder"));


ContentType folderContentType = result.FirstOrDefault();

ListItemCreationInformation newItemInfo = new ListItemCreationInformation();

newItemInfo.UnderlyingObjectType = FileSystemObjectType.Folder;

newItemInfo.LeafName = correspondance;

ListItem newListItem = documentLib.AddItem(newItemInfo);

newListItem["ContentTypeId"] = folderContentType.Id.ToString();

newListItem["Title"] = correspondance;




bolReturn = true;


return bolReturn;


No comments: