1

1 - I have to fetch all the items from the Calendar list who have the specific content type. Eg. Content Type Name- "Test" this content type is assigne to multiple calendar lists.

2 - Also have to fetch the all list names and list items who have the specific content type with programmatically with C#.

Thanks!

1

I just tested this in a Console Application:

        static void Main(string[] args)
    {
        using (var context = new ClientContext(webUrl))
        {
            // 1 - Find items in a List by Content Type
            string contentTypeName = "Event";
            string listName = "Calendar 2";
            ListItemCollection items = FindItemsWithContentType(context, listName, contentTypeName);

            // 2 - Find all Lists that Contain Content Type and Items of that Content Type
            ListCollection siteLists = w.Lists;
            context.Load(siteLists);
            context.ExecuteQuery();

            foreach (var list in siteLists)
            {
                if (CheckForContentTypeInList(context, list.Title, contentTypeName) == true)
                {
                    Console.WriteLine(list.Title + " Contains Content Type");
                    items = FindItemsWithContentType(context, list.Title, contentTypeName);
                }
            }
        }
    }

    private static ListItemCollection FindItemsWithContentType(ClientContext context, string listName, string contentTypeName)
    {
        Web w = context.Web;

        var query = new CamlQuery()
        {
            ViewXml = String.Format("<View><Query><Where><Eq><FieldRef Name='ContentType' /><Value Type='Computed'>{0}</Value></Eq></Where></Query></View>", contentTypeName)
        };

        List list = w.Lists.GetByTitle(listName);
        ListItemCollection items = list.GetItems(query);

        context.Load(items);
        context.ExecuteQuery();

        return items;

    }

    private static bool CheckForContentTypeInList(ClientContext context, string listName, string contentTypeName) {

        Web w = context.Web;


        ContentTypeCollection contentTypes = w.Lists.GetByTitle(listName).ContentTypes;

        context.Load(contentTypes);
        context.ExecuteQuery();

        foreach (ContentType ct in contentTypes)
        {
            if (ct.Name == contentTypeName)
            {
                return true;
            }
        }

        return false;
    }

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.