I have created a JavaScript file and deployed across my site collection. The code is supposed to get the content type ID from any upload form, figure out what this content type is and based on this information fill in certain default values in the form.
I have figured out how to get everything but name of the content type given its ID. Could someone provide the JS code that would do this.
Also do I need to include any special references in my file in order to make this work?
I am using SharePoint 2010
Thanks
EDIT:
As per alam235 sugestion, my code is as follows:
var contentTypeCollection;
var contentType;
$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(executeCode, "sp.js"); });
function executeCode() {
if (checkIfForm()) {
var typeID = getID();
sayHello(typeID);
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(typeID);
clientContext.load(contentType);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
}
}
function onQuerySucceeded() {
$("input[title='Name']").val(this.contentType.get_name());
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
function checkIfForm() {
var url = $(location).attr('href');
if (url.indexOf("Upload.aspx?") == -1) {
return false;
}
else {
return true;
}
}
function getID() {
var url = $(location).attr('href');
var start = url.indexOf("ContentTypeId");
start = url.indexOf("0", start);
var end = url.indexOf("&", start);
return url.substring(start, end);
}
This fails when I attempt to get the content type by ID. My best guess is that the content type Id that is found in form Url is not the content type Id that I need to use (which is strange since I am the one who created the Url in the first place using a Web Part).
0x0120008EA1074CE79CD74985B8DADFD15F40A60A0200771CB9AE73C5A44AB3DCD294ADA29C76
That is an example of my content type Id. So the question is, how do I get the content type name from the upload form?