2009-03-11 14 views

Odpowiedz

15

Jeśli szukasz kodu, mam coś dla ciebie. Jeśli chcesz znaleźć wszystkie elementy treści internetowych Zapytanie wtedy można zadzwonić do mojego kodu:

FindWebPart("http://server.com/", "Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart"); 

Oto kod:

public static void FindWebPart(string siteCollectionUrl, string webPartName) 
{ 
    using (SPSite siteCollection = new SPSite(siteCollectionUrl)) 
    { 
     using (SPWeb rootSite = siteCollection.OpenWeb()) 
     { 
      FindWebPartHelper(rootSite, webPartName); 
     } 
    } 
} 

public static void FindWebPartHelper(SPWeb site, string webPartName) 
{ 
    // Search for web part in Pages document library 
    SPList pagesList = null; 
    try 
    { 
     pagesList = site.Lists["Pages"]; 
    } 
    catch (ArgumentException) 
    { 
     // List not found 
    } 

    if (null != pagesList) 
    { 
     SPListItemCollection pages = pagesList.Items; 
     foreach (SPListItem page in pages) 
     { 
      SPFile file = page.File; 
      using (SPLimitedWebPartManager mgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared)) 
      { 
       try 
       { 
        SPLimitedWebPartCollection webparts = mgr.WebParts; 
        foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webparts) 
        { 
         // Here perform the webpart check 
         // For instance you could identify the web part by 
         // its class name 

         if (webPartName == wp.GetType().ToString()) 
         { 
          // Found a match! Now do something... 
          Console.WriteLine("Found web part!"); 
         } 
        } 
       } 
       finally 
       { 
        // Needs to be disposed 
        mgr.Web.Dispose(); 
       } 

      } 
     } 
    } 

    // Check sub sites 
    SPWebCollection subSites = site.Webs; 
    foreach (SPWeb subSite in subSites) 
    { 
     try 
     { 
      FindWebPartHelper(subSite, webPartName); 
     } 
     finally 
     { 
      // Don't forget to dispose! 
      subSite.Dispose(); 
     } 
    } 
} 

Oczywiście można zrobić małe zmiany do tego kodu. Obecnie robi porównanie ciągów, ale łatwo jest to zrobić w bardziej typowy sposób. Baw się dobrze!

+0

mistrz, dzięki za help – nailitdown

+0

Cieszę się, że mogę pomóc :-) – LeonZandman

+0

Przy okazji, czy nie należy oznaczać mojej odpowiedzi jako odpowiedzi, klikając znak wyboru? – LeonZandman

3

drodze alternatywa, jeśli chcesz przetestować strony część internetowych w tym domyślnej strony na serwisach współpracy, można użyć następującego fragmentu kodu, który wykorzystuje właściwość Pliki obiektu SPWeb:

private static void FindWebPart(string siteUrl, string webPartName) 
{ 
    using (var site = new SPSite(siteUrl)) 
    { 
     foreach (SPWeb web in site.AllWebs) 
     { 

      foreach (var file in web.Files.Cast<SPFile>().Where(file => file.Name.EndsWith("aspx"))) 
      { 
       FindWebPartOnPage(webPartName, file); 
      } 

      var pagesTemplateType = (SPListTemplateType)Enum.Parse(typeof(SPListTemplateType), "850"); 
      foreach (var documentLibrary in web.Lists.Cast<SPList>().Where(list => list.BaseTemplate == pagesTemplateType || (list.BaseTemplate == SPListTemplateType.DocumentLibrary && list.Title.Contains("Pages")))) 
      { 
       foreach (var file in documentLibrary.Items.Cast<SPListItem>().Where(item => item.File.Name.EndsWith("aspx")).Select(item => item.File)) 
       { 
        FindWebPartOnPage(webPartName, file); 
       } 
      } 

      web.Dispose(); 
     } 
    } 
} 

private static void FindWebPartOnPage(string webPartName, SPFile file) 
{ 
    using (var webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared)) 
    { 
     if (webPartManager.WebParts.Cast<WebPart>().Any(webPart => webPart.GetType().Name == webPartName)) 
     { 
      Console.WriteLine(file.ServerRelativeUrl); 
     } 

     webPartManager.Web.Dispose(); 
    } 
} 

Uwaga: biblioteka stron utworzona przez funkcję publikowania nie ma wartości BaseTemplate w SPListTemplateType.DocumentLibrary; zamiast tego jest reprezentowany przez „ukryte” wartość 850.

To się nazywa podobny sposób do odpowiedzi LeonZandman jest jednak tylko nazwa klasy jest używane do dostarczania meczu:

FindWebPart("http://yoursite.com/", "MyWebPart"); 
Powiązane problemy