3

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?

1 Answer 1

4

As you have written that you are not able to get Content Type Name even though you have got the Content type id.

For this you can make use the below code

    var contentTypeCollection;
    var contentType;

    function getContentTypebyId() {
        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");
            clientContext.load(this.contentType);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }
    }

    function onQuerySucceeded() {
        alert(this.contentType.get_name())
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }</script>

3
  • Please see my EDIT in the original post
    – Eugene
    Feb 20, 2013 at 20:08
  • @Eugene Does this work if you run it in the web browser console? Feb 20, 2013 at 20:39
  • I figured out the problem. Your code is absolutely correct. My problem came from the way I set the content type ID in the Url in my web part.
    – Eugene
    Feb 21, 2013 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.