locked
SPList.GetItems() and returned items content type RRS feed

  • Question

  • I"m trying to create a custom asp.net form in SharePoint 2010 Foundation. I have a document library that has multiple content types associated to it. When I query this document library using the SPList.GetItems(SPQuery query) method, I'm returned the documents that I expect. However, when I iterate through each document to test against it's Content Type (SPListItem.ContentTypeId), the ContentTypeID is completely different from what I expect.

    When i iterate through the SPList.Items collection, the content type ID assigned to the document is correct (as inherited from the global content type etc): 0x0101001E61D4F859DC4F9686EC7C0BF0411191001F65EAF85CBE2F4A9573623536CA2D35.

    When I query the SPListItem.ContentTypeId when this document is returned as a "GetItems(query)" result, it's content type is: 0x00C2208B8CE6E1422CADC1C521EAB2A68B

    ...
    create query object
    ...
    
    foreach (SPListItem result in documentLibrary.Items)
    {
       SPContentTypeId ctid = result.ContentTypeId;
       // Content Type ID is correct here!!
    }
    
    foreach (SPListItem result in documentLibrary.GetItems(query))
    {
       SPContentTypeId ctid = result.ContentTypeId;
       // Content Type ID is INCORRECT here!!
    }
    

    When I view the document in the GUI it correctly shows that the content type is assigned.

    Is there something different about an SPListItem that is returned from an SPList.GetItems(query) call compared to a an SPListItem when iterating through SPList.Items?

    Wednesday, December 29, 2010 5:38 AM

Answers

  • I eventually managed to filter by content type beforehand, I mean in the SPQuery that I pass to the List.GetItems(SPQuery).

     

       SPQuery myQuery = new SPQuery();
    
       System.Text.StringBuilder oSb = new System.Text.StringBuilder();
    
    
    
       oSb.Append("  <Where>");
    
       oSb.Append("   <Or>");
    
       oSb.Append("    <Eq>");
    
       oSb.Append("     <FieldRef Name=\"ContentType\" />");
    
       oSb.Append("     <Value Type=\"Computed\">custom ctype name</Value>");
    
       oSb.Append("    </Eq>");
    
       oSb.Append("    <Eq>");
    
       oSb.Append("     <FieldRef Name=\"LookupMilestoneID\" LookupId=\"TRUE\" />");
    
       oSb.Append("     <Value Type=\"Lookup\" >" + myId + "</Value>");
    
       oSb.Append("    </Eq>");
    
       oSb.Append("   </Or>");
    
       oSb.Append(" </Where>");
    
       string sResult = oSb.ToString();
    
       myQuery.Query = sResult;
    
    
    
       System.Text.StringBuilder oSb2 = new System.Text.StringBuilder();
    
    
    
       oSb2.Append("   <FieldRef Name=\"ID\" />");
    
       oSb2.Append("   <FieldRef Name=\"ContentType\" />");
    
       oSb2.Append("   <FieldRef Name=\"myField1\" />");
    
       oSb2.Append("   <FieldRef Name=\"myField2\" />");
    
       oSb2.Append("   <FieldRef Name=\"myField3\" />");
    
    
    
       string s2Result = oSb2.ToString();
    
       myQuery.ViewFields = oSb2.ToString();
    
    
    
    

    I guess this failed content type recognition it's a SharePoint 2010 bug.

    I hope this can help other people having the same problem!


    Thursday, August 11, 2011 8:18 AM

All replies

  • Hi,

     

    According to SPList.Items Property:

     

    Items property returns all the files in a document library, including files in subfolders, but not the folders themselves, it is best practice is to use one of the GetItem* methods of SPList to return a filtered collection of items.

     

    Both 2 approaches could retrieve SPListItemCollection object, so please check if the items are of the scope.

    GetItems would return item under query  CAML condition; SPList.Items would return all items, so please check the return item is the same one and if there are more than 1 content types in your list.

    Hope this can help.

     

    Best Regards,

    Aaron

    Thursday, December 30, 2010 7:51 AM
  • 0x00 content type looks like some system things but not an item or a folder, it doesn't inherite the "Item" content type of SharePoint.

    You can use content type id to recgnize your items, all document items have a content type id begins with "0x0101" (which is the "Document" content type of SharePoint itself).

     

    Hope it's helpful.

     

    Erucy

    Thursday, December 30, 2010 10:50 AM
  • Thanks for the responses guys. However I'm not sure you understand my issue. I understand how content type ID's work, and the item is placed into the document library and the correct content type is associated correctly. I'll try rephrasing the issue:

    When I iterate through the collection of items the content type ID for the item i want is correct (0x0101001E61D4F859DC4F9686EC7C0BF0411191001F65EAF85CBE2F4A9573623536CA2D35). However, when that same item is returned via the SPList.GetItems() method call, the content type ID has changed for the same item (0x00C2208B8CE6E1422CADC1C521EAB2A68B) and all other properties seem to be correct.

    The question is: Why does the same item have a different content type ID when retrieved via the SPList.GetItems() method as opposed to iterating through the collection of list items on the SPList object?

    Friday, December 31, 2010 12:04 AM
  • Hi Proudest Monkey,

     

    I got the same issue here... I have  Custom List definition with associated three content types (just to make things more difficult all of the three inherits from another custom content type :-) ) and just for one of them, the content type ID of the item retrieved from a GetItems(mySPQuery) is mistaken! And the listItem.ContentType property is null.

    And is of course the ctype id of that listItem is 0x00C2208B8CE6E1422CADC1C521EAB2A68B (by googling it I found this thread).

    In another part of my code, I use the same test (listItem.ContentType.Parent.Id.Equale(....)  and works fine but that listItem comes from a GetItemById(int id).

     

    Even though from SharePoint Manager the Content type of that item is assigned correctly and  has the correct id, inherits from the correct parent content type ecc...

     

    Do you eventually find what was the cause of your problem?

     

    Thank you in advance.

    Arturo Bardelli





    Wednesday, August 10, 2011 2:57 PM
  • I eventually managed to filter by content type beforehand, I mean in the SPQuery that I pass to the List.GetItems(SPQuery).

     

       SPQuery myQuery = new SPQuery();
    
       System.Text.StringBuilder oSb = new System.Text.StringBuilder();
    
    
    
       oSb.Append("  <Where>");
    
       oSb.Append("   <Or>");
    
       oSb.Append("    <Eq>");
    
       oSb.Append("     <FieldRef Name=\"ContentType\" />");
    
       oSb.Append("     <Value Type=\"Computed\">custom ctype name</Value>");
    
       oSb.Append("    </Eq>");
    
       oSb.Append("    <Eq>");
    
       oSb.Append("     <FieldRef Name=\"LookupMilestoneID\" LookupId=\"TRUE\" />");
    
       oSb.Append("     <Value Type=\"Lookup\" >" + myId + "</Value>");
    
       oSb.Append("    </Eq>");
    
       oSb.Append("   </Or>");
    
       oSb.Append(" </Where>");
    
       string sResult = oSb.ToString();
    
       myQuery.Query = sResult;
    
    
    
       System.Text.StringBuilder oSb2 = new System.Text.StringBuilder();
    
    
    
       oSb2.Append("   <FieldRef Name=\"ID\" />");
    
       oSb2.Append("   <FieldRef Name=\"ContentType\" />");
    
       oSb2.Append("   <FieldRef Name=\"myField1\" />");
    
       oSb2.Append("   <FieldRef Name=\"myField2\" />");
    
       oSb2.Append("   <FieldRef Name=\"myField3\" />");
    
    
    
       string s2Result = oSb2.ToString();
    
       myQuery.ViewFields = oSb2.ToString();
    
    
    
    

    I guess this failed content type recognition it's a SharePoint 2010 bug.

    I hope this can help other people having the same problem!


    Thursday, August 11, 2011 8:18 AM
  • Hello,

     

    I tried and managed to reproduce the same thing, but by adding also "<FieldRef Name=\"ContentTypeId\" />" to ViewFields, I received the correct result.

    Don't forget to add useful fields when performing query with custom ViewFields, because you limit the returned data by doing it ;)

    Hope that this solves your problem :)

     

    Best regards,

    Mladen Tisaj

    • Proposed as answer by MMMan2010MVP Thursday, May 3, 2012 6:09 AM
    Thursday, August 11, 2011 3:14 PM
  • I managed to figure out that this special value for a content type (0x00C2208B8CE6E1422CADC1C521EAB2A68B) is actually the null content type.  I blogged the details at the URL below.

    http://mmman.itgroove.net/2012/05/coding-challenge-unknown-contenttypeid/


    Colin Phillips | Twitter: @itgroove_colin | Blog: http://mmman.itgroove.net

    Please click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you.


    Thursday, May 3, 2012 4:36 AM
  • Thanks for reference  :)

    Also nice blog post there ;)


    Please don't forget to mark the post as Helpful if you find my comment useful and as Answer if it solved your problem.

    Thursday, May 3, 2012 9:43 AM
  • Just be aware that you'll never get an "exact" contentypeID match. When the items are created in the list/lib what you'll actually get a  list scoped contentType that inherits from the ContentType in the Contenttype catalog -- there are some "bestmatch" methods that will probably get you what you want.


    All science is either physics or stamp collecting

    Thursday, May 3, 2012 4:25 PM