vendredi 23 mars 2012

Sharepoint: Creating a DocSet programmatically (client context,Silverlight side))

If you surf arround the web looking for a code to create a "Document Set" Item ina sharepoint platform using ClientContext you will get bored by unusfull solution. So I decided to help  you:


if (DocumentSet_IsEnabled(container.Title))
{
 ListItem docSet = null;
 List list = clientContext.Web.Lists.GetByTitle(container.Title);
 clientContext.Load(list);
 clientContext.ExecuteQuery();
 docSet = DocumentSet_Create(list, folder, itemName);
}

public bool DocumentSet_IsEnabled(string listName)
{
 List list = this.Context.Web.Lists.GetByTitle(listName);
 ContentTypeCollection listContentTypes = list.ContentTypes;
 Context.Load(listContentTypes, types => types.Include(type => type.Name)); 
 var result=Context.LoadQuery(
 listContentTypes.Where(
 c => c.Name == "Document Set" || c.Name == "Ensemble de documents"));
 Context.ExecuteQuery();
 return result.Count() == 1;
}

public ListItem DocumentSet_Create(List list, Folder folder, String itemNamestring description = "")
{
 ListItem docSet = null;
 ListItemCreationInformation newItemInfo = new ListItemCreationInformation();
 ContentType contentType = null;    
 newItemInfo.UnderlyingObjectType = FileSystemObjectType.Folder;
 newItemInfo.LeafName = itemName;
 if (folder != null)
  newItemInfo.FolderUrl = folder.ServerRelativeUrl;
 
 docSet = list.AddItem(newItemInfo);
 contentType = this.DocumentSet_GetContentType(list);
 docSet["ContentTypeId"] = contentType.Id.ToString();
try
{
 docSet.Update();
 Context.ExecuteQuery();
}
catch
{docSet = null;}
 return docSet;
}

Aucun commentaire:

Enregistrer un commentaire