SharePoint: LINQ query to get child content types

I had been putting off using / learning linq for a little while, but am now finally writing some .net 3.5 code for SharePoint where there are some clear uses for it. I am finding it really intuitive.

Anyway, below is one of my first real-world linq queries – it is a simple query to get all of the descendant content types from a given content type (note that the parent content type is also included):

SPContentTypeId parentId = new SPContentTypeId(“parentContentTypeId”);

using (SPSite mySiteCollect= new SPSite(“http://url/to/my/spweb”))
{
    using (SPWeb website = siteCol.OpenWeb())
    {
        IEnumerable<SPContentType> descendantContentTypes =
           from SPContentType childCt in myspweb.AvailableContentTypes
           where childCt.Id.IsChildOf(parentId)
           select childCt;

        // Do something here with descendant content types
    }
}

You May Also Like

About the Author: rnowik

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.