0

Trying to retrieve the primary and secondary site administrators using javascript and show them in the labels on custom Access Denied page. Using the code below(inline javascript) to do that but ExecuteOrDelayUntilScriptLoaded(myfunction, "sp.js"); (also used SP.SOD.ExcuteFunc() but of no use) does not get executed. Am I missing something?

<asp:Content ID="Content5" ContentPlaceHolderId="PlaceHolderMain" runat="server">

<script type="text/javascript">
    SP.SOD.registerSod("sp.js", "\_layouts\SP.js");

   // RegisterSodDep("inplview", "sp.js");
        debugger;
        alert("begin");
        var currentContext;




        alert("before method");


            ExecuteOrDelayUntilScriptLoaded(myfunction, "sp.js");


            function myfunction() {
            alert("Entering into function");
            currentContext = SP.ClientContext.get_current();
            //alert("1");
            var siteColl = clientContext.get_site();
            var rootWeb = siteColl.get_rootWeb();
            this.props = rootWeb.get_allProperties();

            clientContext.load(rootWeb);
            clientContext.executeQueryAsync(onRequestSucceeded, onRequestFailed);
            //alert("end");
        }

        alert("after method");


        //alert("After");
        function onRequestSucceeded() {
            alert("inside onrequest");
            var primarySiteContact = this.props.get_item('GA_PrimarySiteCollectionContact');
            var secondarySiteContact = this.props.get_item('GA_SecondarySiteCollectionContact');
            var elem = document.getElementById('lblPrimary');
            elem.innerHTML = primarySiteContact;
            alert(primarySiteContact);
            var elem2 = document.getElementById('lblSecondary');
            elem2.innerHTML = secondarySiteContact;
            alert(secondarySiteContact);
        }

        function onRequestFailed(sender, args) {
            alert('Error: ' + args.get_message());
        }

 </script>
4
  • SharePoint version? 2010 or 2013 Commented Jun 16, 2016 at 10:57
  • I am working on sharepoint 2010
    – Tarun K
    Commented Jun 16, 2016 at 10:59
  • Did u try _spBodyOnLoadFunctionNames.push("myfunction"); Commented Jun 16, 2016 at 11:03
  • Hey Atish, tried _spBodyOnLoadFunctionNames.push("myfunction"); ..it did enter the function but now getting the error "Unable to get property 'get_current' of undefined or null reference"
    – Tarun K
    Commented Jun 16, 2016 at 11:16

2 Answers 2

0

Change your SP.SOD.registerSod("sp.js", "\_layouts\SP.js"); to SP.SOD.registerSod("sp.js", "/_layouts/sp.js");. You need to use forward slashes instead of backslashes.

Also, try using SP.SOD.executeFunc("sp.js", "SP.ClientContext", myfunction); instead of ExecuteOrDelayUntilScriptLoaded(myfunction, "sp.js");.

4
  • OP said his SharePoint version is 2010. Commented Jun 16, 2016 at 11:02
  • Hi Atish, tried SP.SOD.registerSod("sp.js", "/_layouts/SP.js"); but it did not work
    – Tarun K
    Commented Jun 16, 2016 at 11:07
  • @Tarun K: Check my updated answer.
    – Mihail
    Commented Jun 16, 2016 at 11:14
  • @MihailPopa tried using SP.SOD.executeFunc("sp.js", false, myfunction); but unfortunately it did not work and shows no error while debugging
    – Tarun K
    Commented Jun 16, 2016 at 11:22
0

Please try below code:

var ctx;

 SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);
 function sharePointReady () {

                ctx = new SP.ClientContext.get_current();
                var siteColl = ctx.get_site();
                var rootWeb = siteColl.get_rootWeb();
                this.props =  rootWeb.get_allProperties();
                ctx.load(rootWeb);
                ctx.load(this.props); //need to load the properties explicitly

                ctx.executeQueryAsync(Function.createDelegate(this, gotProperty), Function.createDelegate(this, failedGettingProperty));

}


            function gotProperty() {                
                 var policy = this.props.get_item('GA_PolicyDisplayName');
                 var primarySiteContact = this.props.get_item('GA_PrimarySiteCollectionContact');
                 var secondarySiteContact = this.props.get_item('GA_SecondarySiteCollectionContact');

                 elem = document.getElementById('ga-sitecontact');//Pass your desired object
                 elem.innerHTML = "<b>Policy:</b> " + policy  + " <b>Primary Contact:</b> " + primarySiteContact  + " <b>Secondary Contact:</b> " + secondarySiteContact;
            }

            function failedGettingProperty() {
                // swallow
            }

Reference:

Hope this will help you.

3
  • tried the code provided by you but it does not go past ExecuteOrDelayUntilScriptLoaded(SetGASiteContactInner, "sp.js"); hence SetGASiteContactInner() does not get executed
    – Tarun K
    Commented Jun 16, 2016 at 11:38
  • Please new updated code Commented Jun 16, 2016 at 11:44
  • tried the updated solution provided by you. It seems that SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function() does not run somehow due to which SP.js is not loaded
    – Tarun K
    Commented Jun 16, 2016 at 11:54

Your Answer

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

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