使用站点ID(GUID)使用客户端对象模型递归搜索站点

时间:2013-01-24 13:05:33

标签: caml sharepoint-api sharepoint-clientobject

使用客户端对象模型我正在寻找最有效的方法来搜索SharePoint服务器,并根据其唯一ID(GUID)确定特定的子站点是否存在。我们将GUID存储在外部系统中,因为我们需要返回到站点,GUID是唯一无法更改的属性。我知道CAML可用于搜索特定站点内的数据。但是,我还没有找到能够为子网站执行此操作的API。我被迫做一个递归搜索并使用for循环。就我而言,我们可以在我们的服务器上嵌套数千个站点。

这个逻辑做了一个级别 - 但是当存在数千个子网站时效率不高。

    public bool SiteExists(ClientContext context, string myGuid)
    {
        Web oWebsite = context.Web;
        context.Load(oWebsite, website => website.Webs, website => website.Title, website => website.Description, website => website.Id);
        context.ExecuteQuery();
        for (int i = 0; i != oWebsite.Webs.Count; i++)
        {
            if (String.Compare(oWebsite.Webs[i].Id.ToString(), myGuid, true) == 0)
            {
                return true;
            }
        }
        return false;
    }

1 个答案:

答案 0 :(得分:2)

public bool SiteExists(ClientContext context, string myGuid) {
    Guid id = new Guid(myGuid);
    Site site = context.Site;
    Web foundWeb = site.OpenWebById(id);

    context.Load(foundWeb);
    context.ExecuteQuery();

    if(foundWeb != null) {
        return true;
    }
    return false;
}
相关问题