0

I need to Programatically be able to change the content type of the folders in a SharePoint Online Site.

I have Java Program that needs to run and detect if a folder has the correct Content Type and if not Change it.

This is how i have been triying to do it.

SiteRequestBuilder site = graphClient.sites(siteId); ContentType tfct = null; for (ContentType contentType : site.lists(listId).contentTypes().buildRequest().get().getCurrentPage()) { if (contentType.name.equalsIgnoreCase("Tagged Folder")) { tfct = contentType; break; } } 

Here i detect and search for the Content Type i want the folder to have.

Then i do a recursive crawl by all the items, and if i detect the item is a folder i then need to change the content type.

 request = graphClient .drives(driveId) .items(itemId) .children() .buildRequest(); DriveItemCollectionPage driveItems = request.get(); for (DriveItem driveItem :driveItems.getCurrentPage()){ if (driveItem.folder != null) { ListItem listItem = graphClient.drives(driveId).items(driveItem.id).listItem().buildRequest().get(); if(!listItem.contentType.id.equalsIgnoreCase(tfct.id)){ listItem.contentType.id = tfct.id; graphClient.sites(siteId).lists(listId).items(listItem.id).buildRequest().patch(listItem); } } } 

But now when i run this code i get

[2024-03-25 21:28:04.253] ERROR main com.mcm.sharepoint.SharePointTaggerUtil Error listing drive items: Error code: invalidRequest Error message: Invalid request 

Any one can give me a hand on either what is wrong or how can i change the ContentType and post it

    1 Answer 1

    0

    I was able to do this by following the tips in this page https://brettmckenzie.net/posts/updating-a-sharepoint-list-items-content-type-in-the-microsoft-graph/

    So what i needed to do, was create a new ListItem with the fields i wanted to change and then do a patch on the item with this new listItem.

    Here is the step by step guide.

    1. First find the content Type by looking in the list where your item to update it.
    2. Once you have it create a ContentTypeInfo Object and add the id and name to it.
    3. Create a new ListItem Object and add the contentTypeInfo created to the contentType propertie
    4. Patch to the listItem.

    In this code bellow tfct is of the type ContentType

     ContentTypeInfo contentTypeInfo = new ContentTypeInfo(); contentTypeInfo.id = tfct.id; contentTypeInfo.name = tfct.name; ListItem listItemNewInfo = new ListItem(); listItemNewInfo.contentType = contentTypeInfo; graphClient.drives(driveId).items(driveItem.id).listItem().buildRequest().patch(listItemNewInfo); 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.