Search This Blog

Thursday, May 12, 2016

ECMAScript – Get Current Web ID

OBJECTIVE

To programmatically obtain the Current Web ID, with JavaScript Client Object Model.

NEEDS EXAMPLE
One wants to open a Page inside a SharePoint popup, and pass in query string the current Web ID.

SOLUTION
In this example it retrieves one list by Title from the Root Web.

<input onclick="javascript:init();" type="button" value="Task Selector"/>
<SharePoint:ScriptLink ID="ScriptLink1" Name="sp.js" runat="server" OnDemand="true" Localizable="false" />

<script type="text/ecmascript">
    var options;
        function portal_openModalDialog(pageUrl) {
            options = SP.UI.$create_DialogOptions();
            options.width = 600;
            options.height = 400;
            options.url = pageUrl;
            options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
            SP.UI.ModalDialog.showModalDialog(options);
        } 

        function CloseCallback(result, target) {
            location.reload(true);
        }

        function init()
        {
            var context = new SP.ClientContext.get_current();
            this.Web = context.get_web();
            context.load(this.Web);
            context.executeQueryAsync(Function.createDelegate(thisthis.onSuccess),
                Function.createDelegate(thisthis.onFail));
        }

        function onSuccess(sender, args)
        {
            portal_openModalDialog("http://myserver/Pages/default.aspx?WebID=" + this.Web.get_id());
        }

        function onFail(sender, args) {
            alert('Failed:' + args.get_message());
        }
</script>

No comments:

Post a Comment