Implementation

  1. Get Host Web Context
  2. Get Site Content Types Collection
  3. Define new Content Type
  4. Add the new Content Type to collection

Get Started:

  1. Create a SharePoint –hosted ‘Apps for SharePoint 2013’ project using Visual Studio 2012/2013.

  2. Provide required permission to App for creating Content Type in AppManifest.xml.

  3. Add a button on page to create Content Type.
    1. <%-- The markup and script in the following Content element will be placed in the <body> of the page --%>
    2. <asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
    3. <div>
    4. <input type="button" id="btnCreateContent Type" value="Create Content Type" onclick="createContentType()" />
    5. </div>
    6. </asp:Content>
  4. Modify App.js file and replace its content by the following code.
    1. 'use strict';
    2. var context = SP.ClientContext.get_current(); //App Web Context
    3. var hostWebUrl, hostWebContext;
    4. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
    5. $(document).ready(function () {
    6. //Get the URI decoded URLs.
    7. hostWebUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    8. hostWebContext = new SP.AppContextSite(context, hostWebUrl);
    9. });
    10. // Retrieve a query string value.
    11. function getQueryStringParameter(paramToRetrieve) {
    12. var params = document.URL.split("?")[1].split("&");
    13. for (var i = 0; i < params.length; i = i + 1) {
    14. var singleParam = params[i].split("=");
    15. if (singleParam[0] == paramToRetrieve) return singleParam[1];
    16. }
    17. }
    18. //Create Content Type
    19. function createContentType() {
    20. if (hostWebContext != undefined && hostWebContext != null) {
    21. var hostWeb = hostWebContext.get_web();
    22. var contentTypeCollection = hostWeb.get_contentTypes();
    23. //Get Document Content type by its Id. We will use Document Content Type as Base Content Type
    24. //to create a new Content Type.
    25. //Refer article for Ids of other Content Types.
    26. var contentType = contentTypeCollection.getById("0x0101");
    27. //Creating new Content Type
    28. var newContentType = new SP.ContentTypeCreationInformation();
    29. newContentType.set_name('Employee');
    30. newContentType.set_group('Employee Details');
    31. newContentType.set_description('Content Type for Employee Details.');
    32. //Set Base Content Type
    33. newContentType.set_parentContentType(contentType);
    34. contentTypeCollection.add(newContentType);
    35. context.load(contentTypeCollection);
    36. context.executeQueryAsync(
    37. function () {
    38. alert('Content type created successfully on Host Web.');
    39. },
    40. function onContenttypeFailed(sender, args) {
    41. alert('Content type creation failed. Error: ' + args.get_message() + '\n' + args.get_stackTrace());
    42. });
    43. }
    44. }
  5. Build and Deploy the solution.

  6. Click on ‘Create Content Type’ button, Content Type will be created on Host Web.

    Create Content Type

  7. Navigate to Host Web Site Content types, you will see ‘Employee’ Content Type under ‘Employee Details’ group.

    Employee Details

Additional Data

The following table shows Content Type Ids, which can be used as Base Content Type.

Table

Hierarchy:

Hierarchy

Conclusion

Using this JSOM, you can create Content Type on Host Web. By using only App Web context, you can create Content type on App Web. To use any other Content type as Base Content type, you can refer to the above table. If you do not provide any base Content type, by default it is set to ‘Item’ Content type.