Search code examples
asp.netadd-incsomsharepoint-onlinesharepoint-jsom

Get SharePoint ClientContext via CSOM/JSOM in SharePoint Online Add-in


I have looked everywhere for a solution to this issue and have come up short. If someone could tell me the trick I would be greatful.

Scope: I am building a SharePoint add-in for SharePoint Online. In the appweb I am trying to use a web part to get the ClientContext of a ListItem via CSOM or JSOM. I cannot use a sandbox solution.

Main Goal: I need ClientContext so I can get the body of a ListItem, use a GetBytes, and say zip or UTF8 encode the body. I also need to use SP.ListOperation.Selected.getSelectedItems(clientContext) in order to select multiple ListItems that have been selected by the user.

Code Example (Not complete):

 <script>
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sharePointReady);

        function helloWorldTest() {
            alert("Function helloWorldTest Active");
            var listURL = '/sites/dev/';
            var clientContext = new SP.ClientContext(listURL);
            var olist = clientContext.get_web().get_lists().getByTitle('Secure List')

            var currentLib = web.get_lists().getById(currentlibid); //Gets the current Library
            var selectedItems = SP.ListOperation.Selection.getSelectedItems(clientContext);
            for (var i in selectedItems) {
                var currentItem = currentLib.getItemById(selectedItems[i].id);
                context.load(currentItem);
                for (i in items) {
                    selItems += '|' + selectedItems[i].id;
                }


            }
        }


                /*
                var itemCreateInfo = new SP.ListItemCreationInformation();
                this.oListItem = oList.addItem(itemCreateInfo);

                oListItem.set_item('Title', 'Test');
                oListItem.set_item('Body', 'Hello World!');

                oListItem.update();

                clientContext.load(oListItem);

                clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
                alert("fuction complete");
            }

            function onQuerySucceeded() {

                alert('Item created: ' + oListItem.get_id());
            }

            function onQueryFailed(sender, args) {

                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            }
        }
        */
    </script>

I would really appreciate the help. Thank you.


Solution

  • If you are trying to get a client context for the current site then you can just use:

    SP.ClientContext.get_current()
    

    If you are trying to build a client context to reach into the host web from the app web, then you need to use the full URL of the host web. In the code above it looks like you are constructing the client context like this:

    var listURL = '/sites/dev/';
    var clientContext = new SP.ClientContext(listURL);
    

    This is a relative URL and won't work when creating a client context. Normally, the URL to the host web is available as a query string parameter named SPHostUrl, so you may have to grab it from there. And know that it has to be the URL to the site alone e.g. http://server/sites/dev/ and not something like http://server/sites/dev/default.aspx or another URL to a resource in the site.