Advertisement
Guest User

Untitled

a guest
Aug 10th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.SharePoint;
  4.  
  5. namespace SharePoint2010DevConsoleApp
  6. {
  7.   class Program
  8.   {
  9.     public static SPList TryGetListFromWeb(SPWeb web, string listUrl)
  10.     {
  11.       Console.WriteLine(string.Format("Looking for list {0} on web {1}.", listUrl, web.Url));
  12.  
  13.       //int websCount = web.Webs.Count; //This line of code is purely to stop the infinite loop bug that calling web.GetList seems to cause.
  14.       SPList returnList = null;
  15.  
  16.       try
  17.       {
  18.         returnList = web.GetList(listUrl);
  19.  
  20.         Console.WriteLine("List found");
  21.       }
  22.       catch
  23.       {
  24.         returnList = null;
  25.         Console.WriteLine("List not found");
  26.       }
  27.  
  28.       return returnList;
  29.     }
  30.  
  31.     public static void PropogateContentTypeChangesOnWeb(SPWeb web, SPContentType sourceContentType, bool updateFields, bool removeFields)
  32.     {
  33.       Console.WriteLine("PropogateContentTypeChangesOnWeb: " + web.Url);
  34.  
  35.       IList<SPContentTypeUsage> sourceContentTypeUsages = SPContentTypeUsage.GetUsages(sourceContentType);
  36.  
  37.       if (sourceContentTypeUsages.Count > 0)
  38.       {
  39.         foreach (SPContentTypeUsage usage in sourceContentTypeUsages)
  40.         {
  41.           if (usage.IsUrlToList)
  42.           {
  43.             SPList list = TryGetListFromWeb(web, usage.Url);
  44.           }
  45.         }
  46.       }
  47.  
  48.       //Process sub webs
  49.       foreach (SPWeb subWeb in web.Webs)
  50.       {
  51.         PropogateContentTypeChangesOnWeb(subWeb, sourceContentType, updateFields, removeFields);
  52.         subWeb.Dispose();
  53.       }
  54.     }
  55.  
  56.     static void Main(string[] args)
  57.     {
  58.       string siteAddress = "http://localhost";
  59.  
  60.       using (SPSite site = new SPSite(siteAddress))
  61.       {
  62.         using (SPWeb currentWeb = site.OpenWeb())
  63.         {
  64.           SPContentType pageCommentContentType = currentWeb.ContentTypes["Page Comment"];
  65.  
  66.           if (pageCommentContentType != null) // We have a content type
  67.           {
  68.             PropogateContentTypeChangesOnWeb(currentWeb, pageCommentContentType, true, false);
  69.           }
  70.           else // No content type is found.
  71.           {
  72.             Console.WriteLine("The content type does not exist in this site collection.");
  73.           }
  74.         }
  75.       }
  76.     }
  77.   }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement