0

I have created WebPart in which I'm getting list item.

I'm passing list name manually. Now I'm trying to get all list which are associated with SPContentType. I have created custom SPContentType with name "Birthday".

1

You can use the SPList.ContentTypes property to check if it contains your custom content type "Birthday" and apply your business logic to that list

An example from MSDN: https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontenttypecollection.bestmatch.aspx

    using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite siteCollection = new SPSite("http://localhost"))
         {
            using (SPWeb webSite = siteCollection.OpenWeb())
            {
               // Get a content type id to search for.
               SPContentTypeId contentTypeId = SPBuiltInContentTypeId.Task;

               foreach (SPList list in webSite.Lists)
               {
                 if (ListContains(list, contentTypeId))
                    Console.WriteLine("The {0} list uses the content type.", 
                                       list.Title);
               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }

      static bool ListContains(SPList list, SPContentTypeId id)
      {
         SPContentTypeId matchId = list.ContentTypes.BestMatch(id);
         return matchId.IsChildOf(id);
      }
   }
}

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.