In this blog I would like to share code to get the content type collection from APP host web in SharePoint Hosted app using JavaScript Object Model.
- var ctx;
- var appCtxSite;
- var contentTypeCollection;
- var contentType;
- var hostWebURL;
- var appWebURL;
- var contentTypeName;
- var ContentTypeID;`
- var ContentTypeEnumerator;
-
-
- $(document).ready(function () {
-
-
- hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
- appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
-
- getHostWeb();
- });
-
-
-
- function manageQueryStringParameter(paramToRetrieve)
- {
- var params = document.URL.split("?")[1].split("&");
- var strParams = "";
- for (var i = 0; i < params.length; i = i + 1)
- {
- var singleParam = params[i].split("=");
- if (singleParam[0] == paramToRetrieve)
- {
- return singleParam[1];
- }
- }
- }
-
-
- function getHostWeb()
- {
-
- ctx = new SP.ClientContext(appWebUrl);
-
- appCtxSite = new SP.AppContextSite(ctx, hostWebUrl);
-
- if (ctx != undefined && ctx != null)
- {
- web = appCtxSite.get_web();
- }
-
- ctx.load(web);
- ctx.executeQueryAsync(getContentTypeCollection, onQueryFailed);
-
- }
-
- function getContentTypeCollection()
- {
-
- contentTypeCollection = web.get_contentTypes();
-
- ctx.load(contentTypeCollection);
- ctx.executeQueryAsync(getallContentType, onQueryFailed);
- }
-
- function getallContentType()
- {
- ContentTypeEnumerator = contentTypeCollection.getEnumerator();
- var count = contentTypeCollection.get_count();
- if (count > 0)
- {
-
- while (ContentTypeEnumerator.moveNext())
- {
- contentType = ContentTypeEnumerator.get_current();
- contentTypeName = contentType.get_name();
- alert(contentTypeName);
- }
- }
- }
-
- function onQueryFailed(sender, args)
- {
- alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }
Summary
In this blog we have explored how to get the all content types from SharePoint Hosted web using JavaScript Object Model.
Happy Coding