Showing posts with label document sets. Show all posts
Showing posts with label document sets. Show all posts

Wednesday, February 20, 2013

SharePoint 2010 : Content types that are available to this Document Set have been added or removed. Update the Document Set.

Sometimes when we click on a document set and within the same we see a yellow line reading 
Content types that are available to this Document Set have been added or removed. Update the Document Set.
The reason behind it is that this document set is not created using the OOB way but using some kind of code practices. Ideal way to create document set is by using 

- DocumentSet.CreateDocumentSet

which sets the docset_LastRefresh property. But doing it in any other way doesn't set this property resulting in the above warning message. To get over with this, we should manually write the code to set the property and prevent seeing this warning in every document set created this way.

SPFolder docSet = web.GetFolder("site/doc lib/folder");
docSet.Item.Properties["docset_LastRefresh"] = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.UtcNow);
docSet.Item.SystemUpdate(false);


Friday, February 8, 2013

SharePoint 2010 : Copying Document sets programatically from one library to another with metadata


Many of you must have come across situations where you have to move the already created Document sets (with a whole bunch of documents in it) from one location to another due to organization re-structure or may be introduction of some new ways of working.
I did come across similar situation where I had to move 5000 Document Sets with documents in them counting 60,000 in total from one library to another. In my case the document sets are within a folder in the source library. Since I am not a folder-loving developer, I tend not to use them frequently.

So here is my code to copy the document sets from source library and put it into the destination library without folders.
Please note:

  • Metadata columns (for Document Set and Document) should read same in the destination library and the source library if you want them to be copied along the process
  • Make sure you have set up Document Set content types in the destination library
I am using a console application so add reference to  
  • Microsoft.SharePoint
  • Microsoft.Office.DocumentManagement.DocumentSets
and make sure your target machine and build is set to proper values.

using (SPSite site = new SPSite("http://sharepointsite"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPFolder folder = web.GetFolder("folderURL"); // sharepointsite/library/folder

                    SPList sourceList = web.Lists["sourcelistname"];

                    SPQuery query = new SPQuery();                    
                    query.Folder = folder;


                    SPListItemCollection itemColl = sourceList.GetItems(query);
                    foreach (SPListItem sourceItem in itemColl)
                    {                        

                        if (sourceItem.ContentType.Name == "Document Set")
                        {
                            DocumentSet documentSet = DocumentSet.GetDocumentSet(sourceItem.Folder);

                            SPList targetList = web.Lists["targetListName"];

                            SPContentTypeId contentTypeId = targetList.ContentTypes["destination document library"].Id;

                            byte[] documentSetData = documentSet.Export();

                            string documentSetName = documentSet.Item.Name;

                            SPFolder targetFolder = targetList.RootFolder;

                            Hashtable properties = sourceItem.Properties;

                            DocumentSet.Import(documentSetData, documentSetName, targetFolder, contentTypeId, properties, web.CurrentUser);
                        }
                    }
                }
            }