6

I have a custom document set type and I want to create a custom button on a calendar that allows my user to click a day and create a new set for that day with the date already filled in.

I've got the code to create a custom doc set and that is working. The problem is trying to set any values other than the name of the docset. Here is the working code:

function createFolder(webUrl,listName,folderName,folderContentTypeId, success, error){  
    getListUrl(webUrl,listName,
      function(listUrl) {
          var folderPayload = {
             'Title' : folderName,
             'BaseName' : folderName,
             'Path' : listUrl
          };

          // Create Folder resource
          // the site I found said the "slug" property was the key to making this work
          $.ajax({
                url: webUrl + "/_vti_bin/listdata.svc/" + listName,
                type: "POST",
                contentType: "application/json;odata=verbose",
                data: JSON.stringify(folderPayload),
                headers: {
                   "Accept": "application/json;odata=verbose",
                   "Slug": listUrl + "/" + folderName + "|" + folderContentTypeId
                },
                success: function (data) {
                    success(data.d);
                },
                error: error
          });
      },
      error);
}

I tried adding the properties to the folderPayload like so:

'StartDate' : "1966-07-25T08:32:00.0000",
'DocumentSetDescription' : "simple words"

But neither of those get passed into the newly create doc set. Does anyone know how to set values when creating a new docset (basically a folder)?

7

The following example demonstrates how to create Document Set and set its properties:

function createFolder(webUrl,listName,folderOptions) 
{  
     var folderPayload = folderOptions; 
     folderPayload['Title'] = folderOptions.Url.split('/').pop();
     folderPayload['Path'] = folderOptions.Url.split('/').slice(0, -1).join('/');

     //Create Folder resource
     return $.ajax({
            url: webUrl + "/_vti_bin/listdata.svc/" + listName,
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(folderPayload),
            headers: {
                   "Accept": "application/json;odata=verbose",
                   "Slug": folderOptions.Url + "|" + folderOptions.ContentTypeId
            }
      });
}


function createDocumentSet(webUrl,listName,docSetOptions)
{
    docSetOptions.ContentTypeId = '0x0120D520';
    return createFolder(webUrl,listName,docSetOptions);
}



function updateFolderProperties(folder,properties)
{
    return $.ajax({
         type: 'POST',
         url: folder.__metadata.uri,
         contentType: 'application/json',
         headers: {
                "Accept": "application/json;odata=verbose",
                "X-HTTP-Method": "MERGE",
                "If-Match": folder.__metadata.etag
         },
         data: JSON.stringify(properties),
   });
}

Usage

var webUrl = _spPageContextInfo.webAbsoluteUrl;
var listName = 'Documents';
var docSetOptions = { 
    'Url': '/Documents/Orders2015'
};
createDocumentSet(webUrl,listName,docSetOptions)
.then(function(data){
    var itemProperties = {
              'Description' : 'Orders 2015',
              'StartDate': '2015-06-23T07:00:00Z'
    };
    return updateFolderProperties(data.d,itemProperties)
})
.done(function(){
    console.log('Document Set has been created succesfully'); 
})
.fail(function(error){
    console.log(JSON.stringify(error));
});

Key points:

  1. The example consists of two requests: 1)create a Document Set 2)update Document Set associated ListItem properties
  2. updateFolderProperties function serves for updating Document Set properties
  3. It is assumed that DocumentSetDescription is a built-in Description field of Document Set content type
  • 1
    Thank you this is so awesome. I didn't know about the .then() chaining. I do have a question about getting the returned data into the .done() part of this? At the end of the process I need to put together the url to open the newly created docset for editing. And for that I need the ItemID of the newly created set. – Rothrock Jun 24 '15 at 15:46
  • In the updateFolderProperties part of it I need to update a managed metadata column, but that doesn't seem to work. Any tips for that? – Rothrock Jul 10 '15 at 15:38
0
  1. If you really want to create a document set using Javascript, JSOM/CSOM could be your option. Here is the sample with JSOM: How to create a Document Set in SharePoint 2013 using JavaScript Client Side Object Model.
  2. When you are using listdata.svc make sure, that it uses not field InternalName, but some interesting value similar to display name without spaces and it depends on UI thread language. To make sure what values you should pass, make get call to some item using listdata.svc and look at the name of the corresponding column name in the result:

     webUrl + "_vti_bin/listdata.svc/" + listName + "(" + someItemId + ")"
    
0

Even simpler is to browse:

webUrl + "_vti_bin/listdata.svc/" + listName

This shows the names to use.

(Indended to put this as a comment but wasn't allowed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.