In a current eRoom to SharePoint migration project I wanted to preserve the “Date Created”, “Date Modified”, “Edited By”, and “Created By” fields in the eRoom documents. To do this I created a custom content type (in SharePoint 2010) based on the standard “Document” content type with four new fields to accept the migrated information. I also created a library template that uses this content type as well as the standard “Document” content type. I’ll explain why later in this article.
Once the documents are migrated I need to update the migrated list/library items. What I don’t want is to keep one set of “preserved” fields and another set of SharePoint fields. The SharePoint fields, as you probably guessed, set the author and editor as the individual doing the migration (or impersonated member) and the created and modified dates being the date of migration.
Updating list items is a pretty easy thing to do in SharePoint’s Client Object Model. The following code accomplishes this task.
foreach (SP.ListItem item in lstItems) { //skip items not assigned to the migratedDoc document type if (item.ContentType.ToString() == migType.ToString()) { row = listItems_dtbl.NewRow(); row["List"] = list.Title; title = (!Convert.IsDBNull(item["Title"]) ? item["Title"].ToString() : ""); if (title == "" && item["FileLeafRef"].ToString() != "") { //set title if one does not exist title = item["FileLeafRef"].ToString(); title = title.Replace(item["File_x0020_Type"].ToString(), ""); } sEditor = (!Convert.IsDBNull(item["eEditor"]) ? item["eEditor"].ToString() : ""); sAuthor = (!Convert.IsDBNull(item["eAuthor"]) ? item["eAuthor"].ToString() : ""); try { editor = ctx.Web.EnsureUser(sEditor); ctx.ExecuteQuery(); } catch { editor = ctx.Web.EnsureUser("tlisko"); } try { author = ctx.Web.EnsureUser(sAuthor); ctx.ExecuteQuery(); } catch { author = ctx.Web.EnsureUser("tlisko"); } item["Editor"] = editor; item["Author"] = author; item["Modified"] = item["eModified"]; item["Created"] = item["eCreated"]; //remove values item["eEditor"] = null; item["eAuthor"] = null; item["eCreated"] = null; item["eModified"] = null; item["ContentTypeId"] = docType.Id; item.Update(); } } try { ctx.ExecuteQuery();
“ctx” is my ClientContext, though that is probably self-evident. You may also note that I only process items whose content type is my custom content type “migratedDoc” which is instantiated as “migType.” Only the migrated documents will have this content type.
eRoom documents do not have Titles, so I set a title based on the file name. You can leave Title blank – it isn’t a required field. Continuing through the code you will notice that I check that the names of the editor and author exist in the LDAP (ctx.Web.EnsureUser(sAuthor)). The update of the list/library item will fail if you try to use an editor or author that doesn’t exist.
I don’t really want to have a custom document library – the SharePoint default document library is just fine. So to get the result I want I need to reassign all the migrated items to the standard “Document” type. List items content types have a Name property. Unfortunately you can’t change it – it is read only. What you can change is the ContentTypeId and changing that value changes the content type of the list item. In the code above “docType” is local instance of the “Document” content type.
I said at the beginning of this article that I would explain why I wanted to keep the “Document” content type in addition to the creating a custom content type. The reason is that you can’t assign a list item to a content type if that content type isn’t in the list. That’s also why you can’t move a document from one SharePoint library to another unless its content type is in the target library as well.
Next I want to remove my custom content type from the list altogether. But to do that, I have to make sure that all those extra fields that are in the custom content type are set to null – which I do before changing the content type. The order of resetting these fields and reassigning the content type doesn’t matter. What does matter is that if these extra fields are not set to null, you will be unable to remove the content type from the list.
After processing all the list items I can now remove the custom content type from the list. Easy, two lines of code:
migType.DeleteObject(); ctx.ExecuteQuery();
But that still doesn’t remove the custom fields from the list. More code is needed. Incorporating the delete of the custom content type you have this.
migType.DeleteObject(); list.Fields.GetByTitle("eCreated").DeleteObject(); list.Fields.GetByTitle("eModified").DeleteObject(); list.Fields.GetByTitle("eAuthor").DeleteObject(); list.Fields.GetByTitle("eEditor").DeleteObject(); ctx.ExecuteQuery();
And that’s it! The library looks like the default document library with only the “Document” content type as desired.
0 Comments