【问题标题】:SharePoint Get all sites and all sub sites using SharePoint online REST APISharePoint 使用 SharePoint Online REST API 获取所有网站和所有子网站
【发布时间】:2019-02-02 00:35:52
【问题描述】:

对于 SharePoint Online 连接器,我们使用以下步骤来获取所有网站:

第 1 步:在 SharePoint 实例上创建具有以下权限 xml 的加载项

<AppPermissionRequests>
        <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl"/>
        <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="Read"/>
</AppPermissionRequests>

第 2 步:使用下面的 API 获取所有站点和子站点

https://<site_name>.sharepoint.com/_api/search/query?querytext='contentclass:STS_Site' &rowlimit=100

我们面临的问题

  1. 以上端点返回所有sites, sub sites along with user’s personal site(One drive),而我们需要所有sites and sub sites 仅限。
  2. 请建议读取所有站点、所有子站点、所有文件夹和文件元数据所需的最低权限

我们参考了以下链接:

【问题讨论】:

    标签: sharepoint sharepoint-online sharepoint-rest-api


    【解决方案1】:

    来自 Joel Dsouza 的方法供您参考。

    1.第一个ajax是获取Root Site Title和Relative URL。

    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/site/rootweb?$select=Title,ServerRelativeUrl",
        method: "GET",
        headers: {
            "Accept": "application/json; odata=verbose"
        },
        success: function(rootsite) {
    
        },
        error: function(rootsite) {},
        async: false
    });
    

    2.第二种AJAX是获取Root Site下的所有子站点。

    $.ajax({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/webinfos?$select=ServerRelativeUrl,Title",
        method: "GET",
        headers: {
            "Accept": "application/json; odata=verbose"
        },
        success: function(subsites) {
            $.each(subsites.d.results, function() {
                getSubSites(this.ServerRelativeUrl, this.Title);
            });
    
        },
        error: function(subsites) {},
        async: false
    });
    

    3.这是一个循环遍历子站点并检查更多子站点的递归函数。

    function getSubSites(SubSiteUrl, SubSiteTitle) {
        console.log(SubSiteUrl);
        $.ajax({
            url: _spPageContextInfo.siteAbsoluteUrl + SubSiteUrl + "/_api/web/webinfos?$select=ServerRelativeUrl,Title",
            method: "GET",
            headers: {
                "Accept": "application/json; odata=verbose"
            },
            success: function(subsites) {
    
                $.each(subsites.d.results, function(index) {
                    getSubSites(this.ServerRelativeUrl, this.Title);
                });
            },
            error: function(subsites) {},
            async: false
        });
    }
    

    更多信息:Get All Sites and Sub Sites using REST API

    【讨论】:

    • 我不想为此使用多个 API。正如@gautam 所建议的,我能够得到预期的结果。感谢您的回答
    【解决方案2】:

    您应该在端点中添加一个路径过滤器。

    普通网站集的路径类似于 https://tenant.sharepoint.com,而个人(一个驱动器)网站集的路径类似于 https://tenant-my.sharepoint.com/personal/*

    所以,修改你的端点如下:

    https://<site_name>.sharepoint.com/_api/search/query?querytext='contentclass:STS_Site
    Path:"https://<site_name>.sharepoint.com/*"'&rowlimit=500
    

    这将仅返回以 https://&lt;site_name&gt;.sharepoint.com 路径开头的网站集,并将排除位于不同路径上的 One Drive 网站集。

    【讨论】:

    • 谢谢 Gautam &lt;AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl"/&gt; 能否请您在插件权限部分纠正我。我只需要设置读取权限。请检查更新的问题谢谢
    • 如果它只是用于显示/检索目的,那么读取权限就足够了。但是如果你需要对它做更多的处理,那么你将需要更高的权限
    • 是的,它只用于 /retrieval。我认为以下权限就足够了。 &lt;AppPermissionRequest Scope="http://sharepoint/search" Right="QueryAsUserIgnoreAppPrincipal"/&gt; &lt;AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="Read"/&gt;如有错误请指正
    【解决方案3】:
    https://yoursharepointsite.com/_api/search/query?querytext='(contentclass:STS_Site) (contentclass:STS_Web)'&trimduplicates=false&rowlimit=5000&selectproperties='Title,Url,Path,ParentLink'"
    

    上面的 url 应该为您提供用户有权访问的所有站点和子站点。您可能需要修剪重复项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 2022-01-02
      • 2016-07-17
      • 2017-10-08
      • 1970-01-01
      • 2018-10-16
      相关资源
      最近更新 更多