Question

Hi can someone help me in putting up a anchor tag in SherePoint custom publishing master page. Actually I want to open a site page on click on this anchor tag, this site page will be in the top level site collection under "Pages" gallery. I tried to put a anchor and on click event used "OpenNewFormUrls1()" function to be called which is defined in the script block in the same master page as :

<script type="text/javascript>" 
SP.SOD.executeOrDelayUntilScriptLoaded(OpenNewFormUrls1, 'SP.js');
function OpenNewFormUrls1() {
    var context= SP.ClientContext.get_current();
        var site=context.get_site();
context.executeQueryAsync(Function.createDelegate(this,this.onSuccess), Function.createDelegate(this, this.onFailure));
        context.load(site);
        var serverRelativeURL=site.get_serverRelativeUrl();
        ULS18u: ; 
        var options = { width: 500, height: 500 };
SP.UI.ModalDialog.commonModalDialogOpen(serverRelativeURL + "/Pages/openNew.aspx", options, null, null); 
    }
    function onSuccess(sender, args) {
        }
        function onFailure(sender, args) {
        }
</script>

After these changes the javascript errors start appearing, and on first click in the anchor tag no model dialog open up, whereas 2nd and afterwards click on the anchor tag open up the model dialog with the page, this happens for each new site visit within the site collection. The error message I am getting is as follows : Message: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. URI: http://sp2010/_layouts/sp.runtime.debug.js?rev=f2An00kVlALdMLzcBc1cug%3D%3D Can anyone help me how can I achieve this ? and what I am missing here ? Kindly help.

Was it helpful?

Solution

For what you are trying to achieve, you don't need to use the Client Object Model. From your code above, you use the Client Object Model only to fetch the root site url. But this is already present in the SharePoint page in form of a javascript variable. See: http://blog.tedpattison.net/Lists/Posts/Post.aspx?ID=9

Also, I noticed that you are using the executeOrDelayUntilScriptLoaded function. This function will execute your OpenNewFormUrls1 function on every page load. Not only when you click on the anchor tag. For your current requirement, I don't think you need this function as you don't really need to use the JS Client Object Model.

So considering all the things, your script block should be like this:

<script type="text/javascript>" 

function OpenNewFormUrls1() {

var options = {
        url: _spPageContextInfo.siteServerRelativeUrl + "/Pages/openNew.aspx",
        width: 500,
        height: 500,
        title: "My Page",
    };
    SP.UI.ModalDialog.showModalDialog(options);
 }
</script>

and then call the OpenNewFormUrls1 function on the onclick of your anchor tag.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top