Failed to create folder through Sharepoint API

Andy Cen (IVC) 96 Reputation points
2021-11-02T02:10:52.427+00:00

Request information:
{
"__metadata": {
"type": "SP.Folder"
},
"ServerRelativeUrl": "/PODEV/SAP%20Library/test"
}

Response information:
{
"error": {
"code": "-1, Microsoft.SharePoint.Client.InvalidClientQueryException",
"message": {
"lang": "en-US",
"value": "The property '__metadata' does not exist on type 'SP.Folder'. Make sure to only use property names that are defined by the type."
}
}
}

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
6,016 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
1,673 questions
No comments
{count} votes

Accepted answer
  1. Andy Cen (IVC) 96 Reputation points
    2021-11-11T02:38:51.403+00:00

    I solved the problem.
    The problem with the content-type parameter is in the official documentation
    148402-%E6%88%AA%E5%B1%8F2021-11-11-%E4%B8%8A%E5%8D%88100647.png

    Send the request based on the above example
    148268-%E6%88%AA%E5%B1%8F2021-11-11-%E4%B8%8A%E5%8D%8892549.png
    The server will report an error
    148269-%E6%88%AA%E5%B1%8F2021-11-11-%E4%B8%8A%E5%8D%8892614.png

    Then I refer to your answer and change the content-type parameter to: application/json; odata=verbose
    148330-%E6%88%AA%E5%B1%8F2021-11-11-%E4%B8%8A%E5%8D%8892459.png

    The folder is created successfully
    148388-%E6%88%AA%E5%B1%8F2021-11-11-%E4%B8%8A%E5%8D%8892532.png

    No comments

2 additional answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 14,306 Reputation points Microsoft Employee
    2021-11-02T07:46:20.197+00:00

    Hi @Andy Cen (IVC) ,
    Per my test the "ServerRelativeUrl" should be SAP%20Library/test. There might be something wrong in your request header. Please refer to the following sample to create the folder

    POST https://{site_url}/_api/web/folders  
    Authorization: "Bearer " + accessToken  
    Accept: "application/json;odata=verbose"  
    Content-Type: "application/json"  
    Content-Length: {length of request body as integer}  
    X-RequestDigest: "{form_digest_value}"  
      
    {  
      "__metadata": {  
        "type": "SP.Folder"  
      },  
      "ServerRelativeUrl": "document library/folder name"  
    }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. RaytheonXie_MSFT 14,306 Reputation points Microsoft Employee
    2021-11-04T01:43:21.873+00:00

    Hi @Andy Cen (IVC) ,
    Could you provide us the Request Header? One case when this error often happens is when you set ContentType request header as "application/json" instead of "application/json;odata=verbose". I have tested my code and it works. Please take a reference:

    		function createFolder() {  
    			var folderName = "test99";  
    			var fullUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/folders";  
    			$.ajax({  
    				url: fullUrl,  
    				type: "POST",  
    				data: JSON.stringify({  
    					"__metadata": {  
    						"type": "SP.Folder"  
    					},  
    					"ServerRelativeUrl": "Document Lib/" + folderName  
    				}),  
    				headers: {  
    					"accept": "application/json;odata=verbose",  
    					"content-type": "application/json;odata=verbose",  
    					"X-RequestDigest": $("#__REQUESTDIGEST").val()  
    				},  
    				success: onQuerySucceeded,  
    				error: onQueryFailed  
    			});  
    		}