Quantcast
Channel: Query on .net
Viewing all articles
Browse latest Browse all 26

Programmatically create folder in SharePoint Document library with client object model

$
0
0

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.

publicbool 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);

ctx.Load(folder);

bool exists = false;

try

{

ctx.ExecuteQuery();

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"));

ctx.ExecuteQuery();

ContentType folderContentType = result.FirstOrDefault();

ListItemCreationInformation newItemInfo = newListItemCreationInformation();

newItemInfo.UnderlyingObjectType = FileSystemObjectType.Folder;

newItemInfo.LeafName = correspondance;

ListItem newListItem = documentLib.AddItem(newItemInfo);

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

newListItem["Title"] = correspondance;

newListItem.Update();

ctx.Load(documentLib);

ctx.ExecuteQuery();

bolReturn = true;

}

return bolReturn;

}


Viewing all articles
Browse latest Browse all 26

Trending Articles