Question

J'ai un domaine d'URL qui contient l'URL d'un tableau de discussion que je créerai un nouvel article pour.Ce que cette planche de discussion peut être dans n'importe quel site ou Web.Jusqu'à présent, j'ai deux idées mais j'aimerais savoir s'il y a une manière plus appropriée.

Option 1. String Comparez l'URL donnée avec l'URL pour chaque site épargnant la correspondance la plus proche.Ensuite, utilisez une chaîne Comparer sur MySPSITE.Altwebs pour trouver le bon Web, et enfin utiliser myspweb.getlistFromurl (CoDurl) pour accrocher mon objet de liste.

Option 2. Supprimer récursivement le dernier segment de l'URL jusqu'à ce qu'il fonctionne

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

Je ouvrirais ensuite le spsite et je trouve le spweb approprié dans un Fashon simillar

Les deux méthodes semblent comme si elles nécessiteraient de nombreuses résines.

Était-ce utile?

La solution

Vous êtes sur la bonne voie, vous devez avoir besoin d'ouvrir le site, de vérifier si l'URL est sur une sous-bande, puis recherchez la liste.Voici un exemple (de http://techPunch.wordpress.COM / 2009/06/03 / SharePoint-2007-Get-Splist-Object-par-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;
}

Autres conseils

ou la méthode plus facile de:

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top