none
Get Document Library current item Content Type RRS feed

  • Question

  • All,

    I need to get the current document (current item) content type name from the document library. I tried CSOM which is Async call and hence it is creating some issues while setting the values.

    Basically there are 3 content types in the document libraries and depending upon the content type I need to get the values or uploaded document URL property.

    Example: Normal document content type, the URL property is "FileRef" column and for the hyperlink content type it is "URL". So depending upon the current item content type, I need to get the uploaded document link. 

    Any help using REST API call or other easier way would be great! Thanks!


    Cheers,
    Maruthu
    P.S. : Please click the 'Mark as Answer' button if a post solves your problem! :)
    Explore the SharePoint

    Monday, January 23, 2017 1:38 PM

Answers

  • Hi Maruthu,

    I am not sure if you are dealing with a single or multiple document libraries. If it just querying against a single library, may be filtering by the Content Type is an option since you already know what are the content types available.

    /_api/lists/getbytitle('yourlibraryname')/items?$filter=ContentType eq 'Document'

    Please remember to mark the replies as answers if they help.

    • Marked as answer by Maruthachalam Tuesday, January 24, 2017 8:34 AM
    Monday, January 23, 2017 1:56 PM
  • Hi,

    The following CSOM code snippet for your reference:

    using (ClientContext ctx = new ClientContext("http://sp13"))
    {
    	 
    	var listTitle = "Documents";
    	var query = new CamlQuery();
    	query.ViewXml = "<View Scope='RecursiveAll' />";
    
    	var items = ctx.Web.Lists.GetByTitle(listTitle).GetItems(query);
    	
    	ctx.Load(items, icol => icol.Include(i => i.ContentType));
    	ctx.ExecuteQuery();
    
    	foreach(var item in items)
    	{
    		//get current item content type name
    		var contentType = item.ContentType.Name;
    		Console.WriteLine(contentType);
    	}
    	Console.ReadKey();
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    Tuesday, January 24, 2017 7:02 AM
    Moderator

All replies

  • Hi Maruthu,

    I am not sure if you are dealing with a single or multiple document libraries. If it just querying against a single library, may be filtering by the Content Type is an option since you already know what are the content types available.

    /_api/lists/getbytitle('yourlibraryname')/items?$filter=ContentType eq 'Document'

    Please remember to mark the replies as answers if they help.

    • Marked as answer by Maruthachalam Tuesday, January 24, 2017 8:34 AM
    Monday, January 23, 2017 1:56 PM
  • check How to get default content type of list programmatically:

    SPContentType defaultCT = list.ContentTypes[0];
    first content type in List.ContentTypes collection is default one.

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Monday, January 23, 2017 2:45 PM
  • Thanks for your reply. I hope this will help my requirement.

    But how do I pass the current item (document) and identify the content type? 


    Cheers,
    Maruthu
    P.S. : Please click the 'Mark as Answer' button if a post solves your problem! :)
    Explore the SharePoint

    Tuesday, January 24, 2017 4:47 AM
  • Thanks for your reply, sadomovalex. I am not using server side code and using CSOM.

    Cheers,
    Maruthu
    P.S. : Please click the 'Mark as Answer' button if a post solves your problem! :)
    Explore the SharePoint


    Tuesday, January 24, 2017 4:48 AM
  • Hi,

    The following CSOM code snippet for your reference:

    using (ClientContext ctx = new ClientContext("http://sp13"))
    {
    	 
    	var listTitle = "Documents";
    	var query = new CamlQuery();
    	query.ViewXml = "<View Scope='RecursiveAll' />";
    
    	var items = ctx.Web.Lists.GetByTitle(listTitle).GetItems(query);
    	
    	ctx.Load(items, icol => icol.Include(i => i.ContentType));
    	ctx.ExecuteQuery();
    
    	foreach(var item in items)
    	{
    		//get current item content type name
    		var contentType = item.ContentType.Name;
    		Console.WriteLine(contentType);
    	}
    	Console.ReadKey();
    }

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com


    Tuesday, January 24, 2017 7:02 AM
    Moderator

  • this is CSOM example. In CSOM there is also List.ContentTypes available.

    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    Tuesday, January 24, 2017 2:29 PM