Create document set with custom properties using JSOM

This is article is not about something new, is about how you organize the code to create a document set with custom properties. Let’s imagine I have created a column “Employee” (people or group field type) and associate it with Document Set content type. Of course I want to assign a value to this field when I create a new document set. What I did was to get the pieces of information from the internet and consolidate my code based on these.

Shp.DocumentSet = function () {
    throw 'Cannot instantiate Shp.DocumentSet static class';
}

Shp.DocumentSet.createDocumentSet = function (listName, docSetName, web, properties, success, fail) {
    /// <signature>
    ///	    <summary>Create document set</summary>
    /// 	<param name="listName" type="String" optional="false" mayBeNull="false">Document library name</param>
    /// 	<param name="docSetName" type="String" optional="false" mayBeNull="false">Document set name</param>
    ///     <param name="web" type="SP.Web" optional="false" mayBeNull="true">Web</param>
    ///     <param name="success" type="Function" optional="false" mayBeNull="false">Success</param>
    ///     <param name="error" type="Function" optional="true" mayBeNull="false">Fail</param>
    /// </signature>

    var e = Function.validateParameters(arguments, [{ name: 'listName', type: String, optional: false, mayBeNull: false },
                                                     { name: 'docSetName', type: String, optional: false, mayBeNull: false },
                                                     { name: 'web', type: SP.Web, optional: false, mayBeNull: true },
                                                     { name: 'properties', type: Object, optional: false, mayBeNull: true },
                                                     { name: 'success', type: Function, optional: false, mayBeNull: false },
                                                     { name: 'fail', type: Function, optional: false, mayBeNull: true }], true);
    if (e) throw e;


    var ctx = (typeof web === 'undefined' || web === null) ? SP.ClientContext.get_current() : web.get_context();
    var web = (typeof web === 'undefined' || web === null) ? ctx.get_web() : web;
    var fail = fail || function (err) { alert(err); }

    Shp.DocumentSet._createDocumentSet(listName, docSetName, ctx, web, properties, success, fail);

};

Shp.DocumentSet._createDocumentSet = function (listName, docSetName, ctx, web, properties, success, fail) {

    var list = web.get_lists().getByTitle(listName);
    ctx.load(list);

    var parentFolder = list.get_rootFolder();
    ctx.load(parentFolder);



    var docSetContentTypeID = "0x0120D520";
    var docSetContentType = ctx.get_site().get_rootWeb().get_contentTypes().getById(docSetContentTypeID);
    ctx.load(docSetContentType);

    ctx.executeQueryAsync(function () {
        SP.DocumentSet.DocumentSet.create(ctx, parentFolder, docSetName, docSetContentType.get_id());
        var docSetFolder = web.getFolderByServerRelativeUrl(parentFolder.get_serverRelativeUrl() + '/' + docSetName);
        var docSetFolderItem = docSetFolder.get_listItemAllFields();
        if (properties != null) {
            for (var property in properties) {
                if (properties.hasOwnProperty(property) === true) {
                    docSetFolderItem.set_item(property, properties[property]);
                }
            }
        }
        docSetFolderItem.update();
        ctx.load(docSetFolderItem);
        ctx.executeQueryAsync(function () {
            success(docSetFolderItem);
        }, fail);
    },
    fail);

};

Shp.DocumentSet.registerClass('Shp.DocumentSet');

I have created a static class called Shp.DocumentSet with Shp.DocumentSet.createDocumentSet method, which accepts these parameters:

  • listName: document library name
  • docSetName: document set name
  • docSetName: SP.Web I want to use for current operation. If null, web for current context will be considered.
  • properties: an object containing custom properties I want to update. If null, no custom property will be updated.
  • success: function to be called on success. This will be called passing list item associated with document set folder as a parameter.
  • fail: function to be called on fail.

Let’s see how you can use it.

var docLibraryName = 'Doc library name';
var docSetName = 'Doc set name';
var spWeb = null;
var properties = {};
properties['Employee'] = employeeId;
var success = function(item) {
    // Do something with item
};

var fail = function (err) {
    // Do something with error
};

Shp.DocumentSet.createDocumentSet(docLibraryName, docSetName, spWeb, properties, success, fail);

Creating document set with custom property is now a simple a operation. I do not need write a lot of code each time I need to this.

Advertisements

Type ‘System.Web.UI.WebControls.FontInfo’ does not have a public property named ‘face’

This error was a surprise for me. I was searching on the internet and could not find one similar case. So I started to investigate on my own and asked users what is the last operation they did before this error appeared. It turned out they copy paste some text from Word into a rich text editor in a custom form.

Once I figured out is something related to rich text box editor, I investigate text they copy pasted from Word and it turned out it contains tags like this.

<font face="Arial">Text here</font>

Was clear for me face property generated the error, but still did no explain why error could not be reproduced in default forms. So I paid all my attention to the rich text editor control.

<SharePoint:InputFormTextBox runat="server" ClientIDMode="Static"  TextMode="MultiLine" Height="70px"  RichText="False" EnableViewState="false" RichTextMode="Compatible" ID="SSOWRequiredServices">
					<xsl:value-of select="@_x0028_SSOW_x0029__x0020_Service" disable-output-escaping="yes" />
</SharePoint:InputFormTextBox>

On a first look, you might say everything is correct, but is not quite correct. I did a mistake declaring text property inside the control tags. It should be declared in a proper way like below.

<SharePoint:InputFormTextBox runat="server" ClientIDMode="Static" Text="{@_x0028_SSOW_x0029__x0020_Service}" TextMode="MultiLine" Height="70px"  RichText="False" EnableViewState="false" RichTextMode="Compatible" ID="SSOWRequiredServices">
</SharePoint:InputFormTextBox>