Add ListItem to list folder using Rest-Api

I had a problem adding a ListItem to the specified folder when using the SharePoint 2013 REST api.

In the client object model, it will look like this:

    var creationInfo = new SP.ListItemCreationInformation();
    creationInfo.FolderUrl = "/Lists/MyList/MyFolder";
    var item = list.addItem(creationInfo );



    item.update();

    ctx.executeQueryAsync(
            Function.createDelegate(this, onSuccess),
            Function.createDelegate(this, onFail));

But when I try to install FolderUrl through the recreation service

{ '__metadata' : { 'type': 'SP.Data.MyListListItem' }, 'Title': 'NewItemInFolder', 'FolderUrl': '/sites/example/Lists/MyList/MyFolder' }

I have 400 Bad Request

I also tried adding the ListItem to the List first and then updating FolderUrl, but that also didn't work.

How to add ListItem to a list folder in SharePoint using Rest-Api?

Edit:

 { '__metadata' : { 
            'type': 'SP.Data.MyListListItem',
            'type': 'SP.Data.ListItemCreationInformation'
    }, 
    'Title': 'NewItemInFolder',
    'FolderUrl': '/sites/example/Lists/MyList/MyFolder'
    }

Now I tried to use both ListItemEntityTypeFullNameEntity from my list and ListItemCreationInformation, but I also received only 400 Bad Request.

And when I view the request using Fiddler, I see that SharePoint is now ignoring my Entity List type SP.Data.MyListItem

+4
4

REST api, listdata.svc

url: /_vti_bin/listdata.svc/[Name of List]
Header:Content-Type: application/json;odata=verbose
Body: {Title: "Title", Path: "/ServerRelativeUrl"}

, REST api svc.

+3

: , :

  • ListItem
  • File , ,

function executeJson(options) 
{
    var headers = options.headers || {};
    var method = options.method || "GET";
    headers["Accept"] = "application/json;odata=verbose";
    if(options.method == "POST") {
        headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
    }   

    var ajaxOptions = 
    {       
       url: options.url,   
       type: method,  
       contentType: "application/json;odata=verbose",
       headers: headers
    };
    if("payload" in options) {
      ajaxOptions.data = JSON.stringify(options.payload);
    }  

    return $.ajax(ajaxOptions);
}



function createListItem(webUrl,listTitle,properties,folderUrl){
    var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items";
    return executeJson({
        "url" :url,
        "method": 'POST',
        "payload": properties})
        .then(function(result){
             var url = result.d.__metadata.uri + "?$select=FileDirRef,FileRef";
             return executeJson({url : url});
        })
        .then(function(result){
             var fileUrl = result.d.FileRef;
             var fileDirRef = result.d.FileDirRef;
             var moveFileUrl = fileUrl.replace(fileDirRef,folderUrl);
             var url = webUrl + "/_api/web/getfilebyserverrelativeurl('" + fileUrl + "')/moveto(newurl='" + moveFileUrl + "',flags=1)";
             console.log(url);
             return executeJson({
                  "url" :url,
                  "method": 'POST',
                  });
        });
}

var webUrl = _spPageContextInfo.webAbsoluteUrl;
var listTitle = "Requests";  //list title
var targetFolderUrl = "/Lists/Requests/Archive";  //folder server relative url
var itemProperties = {
    '__metadata': { "type": "SP.Data.RequestsListItem" },
    "Title": 'Request 123'
};


createListItem(webUrl,listTitle,itemProperties,targetFolderUrl)
.done(function(item)
{
    console.log('List item has been created');
})
.fail(function(error){
    console.log(JSON.stringify(error));
});

Gist

0
Microsoft.SharePoint.Client.ClientContext clientContext = new Microsoft.SharePoint.Client.ClientContext("URL");

Microsoft.SharePoint.Client.List list = clientContext.Web.Lists.GetByTitle("FolderName");

var folder = list.RootFolder;

clientContext.Load(folder);

clientContext.Credentials = new NetworkCredential(username, password, domain);
clientContext.ExecuteQuery();

folder = folder.Folders.Add("ItemName");

clientContext.Credentials = new NetworkCredential(username, password, domain);
clientContext.ExecuteQuery();
-1
source
creationInfo.set_folderUrl("http://siteURL/Lists/Docs/Folder1");
-1
source

Source: https://habr.com/ru/post/1530030/


All Articles