Вопрос

У меня есть поле URL, которое содержит URL-адрес дискуссионной доски, которую я буду создавать новый элемент для.Дело в том, что эта дискуссионная доска может быть на любом сайте или в Интернете.Пока у меня есть две идеи, но хотели бы знать, если есть более правильный путь ..

Вариант 1. Строка сравнивает данный URL с URL для каждого сайта, сохраняющего ближайший матч.Затем снова используя строку сравнить на MySPSITE.AllWebs, чтобы найти правильную сеть и, наконец, использовать MySpWeb.getlistfromurl (DENTURL), чтобы захватить свой объект списка.

Вариант 2. Рекурсивно удалить последний сегмент URL, пока он не работает

  private void myFunct(testURL){
    Try{
     using(SPSite mysite = new SPSite(testUrl)){
        //vaild if reached
      }
    }catch{
       int i = testURL.lastIndexOf('/');
       if(i>0){
         testURL= myFunct(testURL.subStr(0, i));
       }else{ testURL = null; }
     }
    return testURL;
   }
.

Я бы открою SPSite и найти правильную SPWEB в Simillar Fashon

Оба метода кажутся так, как будто им потребуется много воспоминаний.

Это было полезно?

Решение

Вы находитесь на правильном пути, вам нужно будет открыть сайт, проверять, если URL-адрес в суб-сети, а затем ищите список.Вот один пример (из http://techpunch.wordpress.com / 2009 / 06/03 / SharePoint-2007-Get-Splist-Object-url / ):

/// <summary>
/// Gets an SPList based on the url to the list
/// </summary>
/// <param name="listUrl">Full url to the list</param>
/// <returns>SPList object, null if list is not found</returns>
public SPList GetListByUrl(string listUrl)
{
    SPList list = null;

    try
    {
        using (SPSite site = new SPSite(listUrl))
        {
            if (site != null)
            {
                // Strip off the site url, leaving the rest
                // We'll use this to open the web
                string webUrl = listUrl.Substring(site.Url.Length);

                // Strip off anything after /forms/
                int formsPos = webUrl.IndexOf("/forms/", 0, StringComparison.InvariantCultureIgnoreCase);
                if (formsPos >= 0)
                {
                    webUrl = webUrl.Substring(0, webUrl.LastIndexOf('/', formsPos));
                }

                // Strip off anything after /lists/
                int listPos = webUrl.IndexOf("/lists/", 0, StringComparison.InvariantCultureIgnoreCase);
                if (listPos >= 0)
                {
                    // Must be a custom list
                    // Strip off anything after /lists/
                    webUrl = webUrl.Substring(0, webUrl.LastIndexOf('/', listPos));
                }
                else
                {
                    // No lists, must be a document library.
                    // Strip off the document library name
                    webUrl = webUrl.Substring(0, webUrl.LastIndexOf('/'));
                }

                // Get the web site
                using (SPWeb web = site.OpenWeb(webUrl))
                {
                    if (web != null)
                    {
                        // Initialize the web (avoids COM exceptions)
                        string title = web.Title;

                        // Strip off the relative list Url
                        // Form the full path to the list
                        //string relativeListUrl = listUrl.Substring(web.Url.Length);
                        //string url = SPUrlUtility.CombineUrl(web.Url, relativeListUrl);

                        // Get the list
                        list = web.GetList(listUrl);
                    }
                }
            }
        }
    }
    catch { }

    return list;
}
.

Другие советы

или более простые метод:

using (var site = new SPSite(url)){
  using (var web = site.OpenWeb()){
    var list = web.GetList(url);
  }
}
.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top