简体   繁体   中英

How do I get Sitecore 6 template name or ID in Javascript?

I am making a 3rd party Javascript tool for Sitecore that needs to modify it's behaviour dependant on the template of the current edited page.

So far I've been using this script to fetch the name of the template:

jQuery(".scEditorSectionPanelCell a[onclick*='shell:edittemplate']")[0].innerHTML.match(/\/?([^/]+?)$/i)[1];

A little explanation: When editing a page there is a paragraph called "Quick info" (apologies if this is incorrectly translated - not working with english Sitecore). Inside this paragraph is a direct link to editing the current template - that's the link I fetch with jQuery:

<td>
  <a href="#" onclick="javascript:scForm.postRequest('','','','shell:edittemplate');return false">
    /sitecore/templates/Imported/[NAME-OF-TEMPLATE]
  </a> - 
  <input class="scEditorHeaderQuickInfoInputID" readonly="readonly" onclick="javascript:this.select();return false"
    value="{[ID-OF-TEMPLATE]}">
</td>

From that I get the name of the template with a regex. If I wanted to I could also get the template ID from the sibling <input> element.

The problem is, the "Quick info" paragraph is not visible for all users.

Any suggestions on how to get the template name or ID when "Quick info" is not visible? I do not have access to the underlying ASP.NET code, so the solution needs to be strictly Javascript.

Sitecore ships with a webservice you can use to get this information. However, I didn't setup the security to test when user doesn't have access to quick info.

You'll need to download the linked soapclient.js file and add a reference to it. It's an adaptation of the soapclient.js floating around. I realized it wasn't working for your needs, so I modified it to work for you.

<script type="text/javascript" src="/resources/javascript/soapclient.js"></script>

Then you'll need to add this js to use the soapclient.

    var url = document.location.href.replace(document.location.pathname, "/sitecore/shell/WebService/Service.asmx");
    var id = "1CB4A1EF-F4C9-4596-9364-430885DDDD00"; //path of item.
    var deep = false //Get All fields
    var dbName = "web" //Which DB you'd like to get the fields from. 

    var credentials = new Object();
    credentials.UserName = "Sitecore\\Admin";
    credentials.Password = "b";
    credentials.CustomData = "";
    var pl = new SOAPClientParameters();
    pl.add("id", id);
    pl.add("deep", deep);
    pl.add("databaseName", dbName);
    pl.add("credentials", credentials);

    SOAPClient.invoke(url, "GetXML", pl, true, GetItemFields_callBack);

    function GetItemFields_callBack(response) {
        console.log(response.sitecore.data.data.item.tid);
    }

If you supply just the ID of the user currently logged in, a password is not needed. If you code in an admin's password, you'll be able to access everything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM