6

I've created several content types that inherit from the document content type in the site content type lists. Please refer to the image below: MyContentType

How do I get these these content types programmatically? Basically what I want to do is to show these content types in drop down lists so that the user can select which document library to upload a file to. I'm stuck here:

 using (SPSite site = new SPSite(SPContext.Current.Site.Url))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPContentType documentContentType = web.AvailableContentTypes[SPBuiltInContentTypeId.Document];
            }
        }

Please provide some guidance. Thanks

1
  • Thanks for the answer guys, Is there any way for me to get my custom content types based on the custom group I have created. In my case the group name is SampleDocContentType. If I get my content type based on the base id, Document. Then it will get all the ContentType that inherit this ContentType even the content types that I don't want them to appear in my dropdownlist. Jan 30, 2012 at 2:15

4 Answers 4

8

I thik that you should create a base content type which is inherited from Document content type. Then you should inherit Arvhitecture Document, Manual Document, etc. content types from your base content type.

After this you can select your content types by base content type like this:

SPContentTypeId id = new SPContentTypeId(BaseContentTypeId);
IList<SPContentType> ContentTypes = web.ContentTypes.Cast<SPContentType>().Where(c => c.Id.IsChildOf(id)).ToList();
0

You could accomplish this using the SPContentType.Parent property.

Your sample code might look like this. I have not executed this code and checked, so modify wherever required.

using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
  using (SPWeb web = site.OpenWeb())
   {
     SPContentTypeCollection allContentTypes = web.ContentTypes;
                SPContentTypeCollection docContentTypes = null;
                foreach (SPContentType objContentType in allContentTypes)
                {
                    if (objContentType.Parent.Name == "Document")
                    {
                        docContentTypes.Add(objContentType);
                    }
                }
                // docContentTypes contains all content types derived from content type "Document". You can implement additional checks and add this into a drop down list.
    }
}
1
  • 1
    if there are several 3rd party solutions in the site which install own lists that are inherited from document type, it can break an logic of our solution.
    – Alexander
    Jan 28, 2012 at 6:41
0

Unfortunately the above code won't work - this is because you can't reference the SPContentTypeCollection if null (and can't initialize it using 'new...' either). Use a list to do it:

using System.Collections.Generic;
using Microsoft.SharePoint;
...
using (SPSite GetSite = new SPSite("http://site"))
{
    using (SPWeb TopWeb = GetSite.OpenWeb())
    {
            SPContentTypeCollection CurContentTypes = TopWeb.ContentTypes;
            List<SPContentType> SelectedTypes = new List<SPContentType>();
            foreach (SPContentType SPCT in CurContentTypes)
            {
                if (SPCT.Group == "MyGroup")
                {
                    SelectedTypes.Add(SPCT);
                }
            }
    }
}

Once this is done, you can simply iterate through the list

0

Why don't you use the Group property of SPContentType class:

using (SPSite siteCollection = new SPSite("put your site url here"))
        {

            using (SPWeb Web = siteCollection.OpenWeb())
            {

                SPContentTypeCollection SPCTCol = Web.ContentTypes;
                List<string> CustomTypesList = new List<string>();
                foreach (SPContentType ContentType in SPCTCol)
                {
                    if (ContentType.Group == "put your content type group name here")
                    {
                        CustomTypesList.Add(Convert.ToString(ContentType.Name));
                    }
                }
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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