【问题标题】:Get SharePoint ClientContext via CSOM/JSOM in SharePoint Online Add-in通过 SharePoint Online 加载项中的 CSOM/JSOM 获取 SharePoint ClientContext
【发布时间】:2015-10-29 08:38:36
【问题描述】:

我到处寻找解决此问题的方法,但都未能解决。如果有人能告诉我诀窍,我会很高兴。

范围:我正在为 SharePoint Online 构建一个 SharePoint 加载项。在 appweb 中,我尝试使用 Web 部件通过 CSOM 或 JSOM 获取 ListItem 的 ClientContext。我不能使用沙盒解决方案。

主要目标:我需要 ClientContext,这样我才能获取 ListItem 的主体,使用 GetBytes,然后说 zip 或 UTF8 对主体进行编码。我还需要使用 SP.ListOperation.Selected.getSelectedItems(clientContext) 来选择用户选择的多个 ListItems。

代码示例(不完整):

 <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>

非常感谢您的帮助。谢谢你。

【问题讨论】:

    标签: asp.net add-in csom sharepoint-online sharepoint-jsom


    【解决方案1】:

    如果您尝试获取当前站点的客户端上下文,那么您可以使用:

    SP.ClientContext.get_current()
    

    如果您尝试构建客户端上下文以从应用程序 Web 访问主机 Web,那么您需要使用主机 Web 的完整 URL。在上面的代码中,您似乎正在像这样构建客户端上下文:

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

    这是一个相对 URL,在创建客户端上下文时不起作用。通常,主机 Web 的 URL 可用作名为 SPHostUrl 的查询字符串参数,因此您可能必须从那里获取它。并且知道它必须是站点的 URL,例如http://server/sites/dev/ 而不是 http://server/sites/dev/default.aspx 或网站中资源的其他 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多