2

I'm migrating a custom list from SharePoint 2010 on-premises to SharePoint Online. The source list has content types enabled and two custom content types associated. When migrating the list, i need to compare the associated content types collections, and add to the destination list all missing content types from the source list.

Here's my code:

var srcCTs = srcList.ContentTypes;
var destCTs = destList.ContentTypes;
var missingCTs = srcList.ContentTypes.Where(ct => !(destList.ContentTypes.Select(dct => dct.<UniqueKey>).Contains(ct.<UniqueKey>)));

where UniqueKey should be a property with unique values. I cannot use properties "Id", "StringId" or "Name" because its values are not unique, so I think I must change the code (or at least the third row)...

How do I check if content type already exists in the destination list?

EDIT: I need to compare both Content Type Collections (srcCTs and destCTs) and extract the difference between them.

1

This will return all the content types into a list

var listContentType = ctx.Web.Lists.GetByTitle('listTitle').ContentTypes

A full example

string siteURL = "http://your_site/";
ClientContext clientContext = new ClientContext(siteURL);
ContentTypeCollection contentTypeColl = clientContext.Web.Lists.GetByTitle("listTitle").ContentTypes;
clientContext.Load(contentTypeColl);
clientContext.ExecuteQuery();

//Display the Content Type name
foreach (ContentType ct in contentTypeColl)
{
    Console.WriteLine(ct.Name);
}

Hope be helpful

  • Thank you for your response, but it's not what I'm looking for. I need to compare two Content Type Collections (coll A and coll B) and extract the difference between them. – Cecilia Dec 1 '16 at 14:28
0

I found a solution using the Parent Content Type (as any association to the list creates a new child instance of the content type).

var missingCTs = srcList.ContentTypes.Where(ct => !(destList.ContentTypes.Select(dct => dct.Parent.StringId).Contains(ct.Parent.StringId))).ToList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.