Add Existing Content Type to The List in SharePoint 2010 Using ECMAScript


Introduction

In this article you will see how to add existing content type to the list in SharePoint 2010 using ECMAScript. I have a list named "List Request" which has the following content types (Navigate to the list, click on List tab in the ribbon interface. Click on List Settings button).

  1. Item.
  2. Folder.
  3. Central Content Type.

I have a custom content type named "Custom Content type" under the "Custom Content Types" group (Navigate to the site, click on Site Actions. Click on Site Settings. In the Galleries section, click on Site Content Types. Click on "Custom Content Type"). I am going to add that content type to the list "List Request" using ECMAScript.

Steps Involved

Step 1 :
 Navigate to the SharePoint site (I have a site page created in my site where I am going to add the content editor web part).

Step 2 : Go to Site Actions, click on Edit Page.

Step 3 : Click on the Insert tab in the ribbon interface and then click on Web Part button.

Step 4 : Select Media and Content in the Categories section and then click on Content Editor.

Step 5 : Click on Add.

Step 6 : The
Content Editor web part will be added to the site page.

Step 7 :
  Click on the down arrow and then click on Edit Web Part.

Step 8 :
Click on "Click here to add new content".

Step 9 :
 Click on Format Text tab in the ribbon interface and then click on HTML drop down

Step 10 :
Click on Edit HTML source.

Step 11:
 The HTML source window will pop up.

Step 12: Copy and paste the following script.

     <script language="ecmascript" type="text/ecmascript">
        var contentTypeCollection;
        var contentType;
        var listCollection;
        var list;
        var listContentTypeColl;
 
        function addExistingContentTypetoList() {
            var clientContext = new SP.ClientContext.get_current();
            if (clientContext != undefined && clientContext != null) {
                var web = clientContext.get_web();
                this.contentTypeCollection = web.get_contentTypes();
                this.contentType = contentTypeCollection.getById("0x0100FFC434A461E47E4EB81D3FD76D42BFBF");
                this.listCollection = web.get_lists();
                this.list = listCollection.getByTitle("List Request");
                this.listContentTypeColl = list.get_contentTypes();
                this.listContentTypeColl.addExistingContentType(contentType);
                clientContext.load(this.listContentTypeColl);
                clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
            }
        } 
        function onQuerySucceeded() {
alert('Content Type added successfully');
                    } 
        function onQueryFailed(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
</script>
    <input id="btnaddExistingContentTypetoList" onclick="addExistingContentTypetoList()"
        type="button" value="Add existing content type" />

 

Step 13 : Click on Ok.

Step 14 : In the ribbon interface click on Save & Close button.

Step 15 : In the content editor web part you can find a button named "Add existing content type"; click on that button.

Step 16 : An alert will pop up.

Reference

SP.ContentTypeCollection.addExistingContentType(contentType) Method - http://msdn.microsoft.com/en-us/library/ee658871.aspx

Summary

Thus in this article you have seen how to add the existing content type to a list in SharePoint 2010 using ECMAScript.

Up Next
    Ebook Download
    View all
    Learn
    View all