06 December 2010

programmatically create document set, add or move files, GetDocumentSetById

How to programmatically create document set, add or move files, GetDocumentSetById


Create a new document set

public static DocumentSet CreateDocumentSet(SPList list, string DocumentSetName)
{
 SPContentType docsetCT = list.ContentTypes["Document Set"];
 Hashtable properties = new Hashtable();
 properties.Add("DocumentSetDescription", "New Document Set");

 SPFolder parentFolder = list.RootFolder;

 DocumentSet docSet = DocumentSet.Create(parentFolder, DocumentSetName, docsetCT.Id, properties, true);

 return docSet;
}

Get a document set by id

public static DocumentSet GetDocumentSetById(this SPList list, int ID)
{
 return DocumentSet.GetDocumentSet(list.GetItemById(ID).Folder);
}

Check to see it the current list item is a Document set

public static bool IsDocumentSet(this SPListItem item)
{
 bool documentSetItem = false;
 DocumentSet documentSet = null;

 if (null != item && item.IsFolder())
 {
  documentSet = DocumentSet.GetDocumentSet(item.Folder);

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

Check to see if the current listitem is a item within a Document Set

public static bool IsDocumentSetItem(this SPListItem item)
{
 bool documentSetItem = false;
 DocumentSet documentSet = null;

 if (null != item && null != item.File)
 {
   documentSet = DocumentSet.GetDocumentSet(item.File.ParentFolder);

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

Move a file to a document set

public static SPFile MoveFile(SPWeb web, string sourceUrl, DocumentSet docSet)
{
 SPFile file = web.GetFile(sourceUrl);
 string destinationFolderUrl = docSet.Folder.Url;

 destinationFolderUrl = (!destinationFolderUrl.EndsWith("/")) ? destinationFolderUrl + "/" : destinationFolderUrl;
 file.MoveTo(destinationFolderUrl + file.Name, true);
 web.Update();

 return web.GetFile(web.Url + "/" + destinationFolderUrl + file.Name);
}

Copy a file to a document set

public static SPFile CopyFile(SPWeb web, string sourceUrl, DocumentSet docSet)
{
 SPFile file = web.GetFile(sourceUrl);
 string destinationFolderUrl = docSet.Folder.Url;

 destinationFolderUrl = (!destinationFolderUrl.EndsWith("/")) ? destinationFolderUrl + "/" : destinationFolderUrl;
 file.CopyTo(destinationFolderUrl + file.Name, true);
 web.Update();

 return web.GetFile(web.Url + "/" + destinationFolderUrl + file.Name);
}

Create a 'link to document' file in a document set

public static SPFile CreateDocumentLink(this SPList list, string documentName, string documentUrl, DocumentSet docSet)
{
 SPFile file = null;
 SPListItem item = null;
 if (list is SPDocumentLibrary)
 {
  SPDocumentLibrary docLib = (SPDocumentLibrary)list;
  if (docLib.ContentTypesEnabled)
  {
   SPContentType myCType = list.ContentTypes["Link to a Document"];
   if (myCType != null)
   {

    //replace string template with values
    string redirectAspx = RedirectAspxPage();
    redirectAspx.Replace("{0}", documentUrl);

    //should change the name of the .aspx file per item
    file = docSet.Folder.Files.Add(documentName + ".aspx", UTF8Encoding.UTF8.GetBytes(redirectAspx));

    //set list item properties
    item = file.Item;


    item["ContentTypeId"] = myCType.Id;
    item.Update();

    if (item["ContentType"].ToString() == "Link to a Document")
    {
     SPFieldUrlValue fieldUrl = new SPFieldUrlValue() { Description = documentName, Url = documentUrl };

     item["URL"] = fieldUrl;
     item.Update();
    }
   }
  }
 }

 return file;
}

public static string RedirectAspxPage()
{
 StringBuilder builder = new StringBuilder();
 builder.Append("<%@ Assembly Name='Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' %>");
 builder.Append("<%@ Register TagPrefix='SharePoint' Namespace='Microsoft.SharePoint.WebControls' Assembly='Microsoft.SharePoint' %>");
 builder.Append("<%@ Import Namespace='System.IO' %>");
 builder.Append("<%@ Import Namespace='Microsoft.SharePoint' %>");
 builder.Append("<%@ Import Namespace='Microsoft.SharePoint.Utilities' %>");
 builder.Append("<%@ Import Namespace='Microsoft.SharePoint.WebControls' %>");
 builder.Append("<html xmlns:mso=\"urn:schemas-microsoft-com:office:office\" xmlns:msdt=\"uuid:C2F41010-65B3-11d1-A29F-00AA00C14882\">");
 builder.Append("<head>");
 builder.Append("<meta name=\"WebPartPageExpansion\" content=\"full\" /> <meta name='progid' content='SharePoint.Link' /> ");
 builder.Append("<!--[if gte mso 9]><SharePoint:CTFieldRefs runat=server Prefix=\"mso:\" FieldList=\"FileLeafRef,URL\"><xml>");
 builder.Append("<mso:CustomDocumentProperties>");
 builder.Append("<mso:ContentTypeId msdt:dt=\"string\">0x01010A00DC3917D9FAD55147B56FF78B40FF3ABB</mso:ContentTypeId>");
 builder.Append("<mso:IconOverlay msdt:dt=\"string\">|docx|linkoverlay.gif</mso:IconOverlay>");
 builder.Append("<mso:URL msdt:dt=\"string\">{0}, {0}</mso:URL>");
 builder.Append("</mso:CustomDocumentProperties>");
 builder.Append("</xml></SharePoint:CTFieldRefs><![endif]-->");
 builder.Append("</head>");
 builder.Append("<body>");
 builder.Append("<form id='Form1' runat='server'>");
 builder.Append("<SharePoint:UrlRedirector id='Redirector1' runat='server' />");
 builder.Append("</form>");
 builder.Append("</body>");
 builder.Append("</html>");
 return builder.ToString();
}

9 comments:

  1. Thanks.. very useful.

    ReplyDelete
  2. item.IsFolder() ? Is this a joke?

    ReplyDelete
  3. nope.. not a joke :) Document Set is nothing but a specialized folder.

    ReplyDelete
  4. Correct way would be:

    bool bIsDocSet = item.ContentType.Id.IsChildOf( SPBuiltInContentTypeId.DocumentSet ) ) continue;

    ReplyDelete
  5. documentSet = DocumentSet.GetDocumentSet(item.Folder); if (null != documentSet) documentSetItem = true;

    doesn't quite work. DocumentSet.GetDocumentSet never returns a null value even if you use DocumentSet.GetDocumentSet(null)

    The following however does seem to work however

    documentSet = DocumentSet.GetDocumentSet(folder);
    if (null != documentSet.Item) documentSetItem = true;

    ReplyDelete
  6. What about updating a property of a document set?

    //get the document set we'd like to update the property of
    SPFolder folder = SPContext.Current.List.GetItemById(CADLibraryDocSetUniqueID).Folder;
    DocumentSet docSet = DocumentSet.GetDocumentSet(folder);

    folder.SetProperty("FieldInternalName", "NewValue");
    folder.Update();

    //the above 'update' doesn't work. Any ideas??
    //I've also tried the following to no avail as well

    docSet.Folder.Item.Properties["FieldInternalName"] = "NewValue";
    docSet.Folder.Item.Update(); //this isn't updating to the new value -- it's reseting to the old value

    ReplyDelete