2

I have for now some code but is not working on all sites because each list has its own listcontenttypeId. It looks like a listcontenttypeId is a combination of the original contenttypeId+[SOMETHING]. What is this thing?? What is the best practice to get the listcontenttype by original contenttypeID?

This is my current code:

// list contenttype
            SPList list = curWeb.Lists.TryGetList("Pagina's");
            if (list != null)
            {
                SPContentType listContentType = list.ContentTypes[new SPContentTypeId("0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D003CE876821EAC4EC8AAD81AC501EBCD0700F65D7E6A5FFA6340BA9EE2673A6EA758")];
                if (listContentType != null)
                {
                    SPField listContentTypeField = listContentType.Fields.TryGetFieldByStaticName("ArticleStartDate");
                    if (listContentTypeField != null)
                    {
                        // set this field required
                        listContentType.FieldLinks[listContentTypeField.Id].Required = required;
                        listContentType.Update();
                    }
                }
            }

This is the original contenttypeId: 0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D003CE876821EAC4EC8AAD81AC501EBCD07

I dont know where this is comming from in the listcontenttypeId: 00F65D7E6A5FFA6340BA9EE2673A6EA758

1

You can see my answer to another question for some further explanation, but basically what is happening is that when you use a content type on a list, SharePoint makes a local copy of the content type on that list, and the list level content type gets its own ID.

Here is an MSDN article on Site vs. List level content types.

In addition, the way child content types get IDs is by using the parent content type ID, appending "00", and then appending a GUID.

Here is an MSDN article about Content Type ID structure.

By the looks of the content type ID you are showing as the "original", it looks like a document content type that already has four levels of inheritance before it gets to the list:

Document: 0x0101

Your first custom document: 0x010100C568DB52D9D0A14D9B2FDCC96666E9F2

Your second document (inheriting from first): 0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF39

Your third (inheriting from second): 0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D

Your fourth (inheriting from third): 0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D003CE876821EAC4EC8AAD81AC501EBCD07

Your list level content type (child of 4th): 0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D003CE876821EAC4EC8AAD81AC501EBCD0700F65D7E6A5FFA6340BA9EE2673A6EA758

Some useful methods to use when trying to work with list level content types when you don't know the ID at the list level and only know the ID of the parent are on the SPContentTypeID class: BestMatch, IsChildOf, and IsParentOf.

In your particular case you could do something like this:

SPList list = curWeb.Lists.TryGetList("Pagina's");
if (list != null)
{
    SPContentTypeId parentID = new SPContentTypeId("the parent ID that you know");
    SPContentType listContentType = null;
    foreach (SPContentType potentialChild in list.ContentTypes)
    {
        if (potentialChild.Id.IsChildOf(parentID))
        {
            listContentType = potentialChild;
            break;
        }
    }
    if (listContentType != null)
    {
        SPField listContentTypeField = listContentType.Fields.TryGetFieldByStaticName("ArticleStartDate");
        if (listContentTypeField != null)
        {
            // set this field required
            listContentType.FieldLinks[listContentTypeField.Id].Required = required;
            listContentType.Update();
        }
    }
}
0

I have implemented this:

SPList list = curWeb.Lists.TryGetList("Pagina's");
            if (list != null)
            {
                IList<SPContentType> contentTypes = list.ContentTypes.Cast<SPContentType>().Where(c => c.Id.IsChildOf(ctId)).ToList();
                foreach (SPContentType listContentType in contentTypes)
                {
                    if (listContentType != null)
                    {
                        SPField listContentTypeField = listContentType.Fields.TryGetFieldByStaticName("ArticleStartDate");
                        if (listContentTypeField != null)
                        {
                            // set this field required
                            listContentType.FieldLinks[listContentTypeField.Id].Required = required;
                            listContentType.Update();
                        }
                    }
                }

            }

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.