SharePoint ECMA Client Object Model JSOM Get ContentType

Code to get ContentType with ECMA Client Object model.

<script type=”text/javascript”>

//Get Page Item from Pages Library with ContentType Name

function GetPageItemContentType() {

var clientContext = new SP.ClientContext.get_current();

var oList = clientContext.get_web().get_lists().getByTitle(“Pages”);

var camlQuery = new SP.CamlQuery();

//we can use this variable to pass dynamic page value
var pagename = “sample.aspx”;

camlQuery.set_viewXml(‘<View><Query><Where><Contains><FieldRef Name=\’FileLeafRef\’ /><Value Type=\’File\’>’ + pagename + ‘</Value></Contains></Where></Query></View>’)

this.collListItem = oList.getItems(camlQuery);

// Include Content Type wich Locads Conent Type
clientContext.load(collListItem, ‘Include(ContentType)’);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure));
}

//On success Meethod
function onSuccess() {
var listEnumerator = collListItem.getEnumerator();
while (listEnumerator.moveNext()) {
var currentItem = listEnumerator.get_current();
var contentType = currentItem.get_contentType();
var contentTypeName = contentType.get_name();
alert(“Contenttype Name :” + contentTypeName);
}
}

//On Fail
function onFailure() {
alert(‘Failure!’);
}

//Call ECMA Script Method
ExecuteOrDelayUntilScriptLoaded(GetPageItemContentType, “sp.js”);

</script>

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s