Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Microsoft.SharePoint;
- namespace SharePoint2010DevConsoleApp
- {
- class Program
- {
- public static SPList TryGetListFromWeb(SPWeb web, string listUrl)
- {
- Console.WriteLine(string.Format("Looking for list {0} on web {1}.", listUrl, web.Url));
- //int websCount = web.Webs.Count; //This line of code is purely to stop the infinite loop bug that calling web.GetList seems to cause.
- SPList returnList = null;
- try
- {
- returnList = web.GetList(listUrl);
- Console.WriteLine("List found");
- }
- catch
- {
- returnList = null;
- Console.WriteLine("List not found");
- }
- return returnList;
- }
- public static void PropogateContentTypeChangesOnWeb(SPWeb web, SPContentType sourceContentType, bool updateFields, bool removeFields)
- {
- Console.WriteLine("PropogateContentTypeChangesOnWeb: " + web.Url);
- IList<SPContentTypeUsage> sourceContentTypeUsages = SPContentTypeUsage.GetUsages(sourceContentType);
- if (sourceContentTypeUsages.Count > 0)
- {
- foreach (SPContentTypeUsage usage in sourceContentTypeUsages)
- {
- if (usage.IsUrlToList)
- {
- SPList list = TryGetListFromWeb(web, usage.Url);
- }
- }
- }
- //Process sub webs
- foreach (SPWeb subWeb in web.Webs)
- {
- PropogateContentTypeChangesOnWeb(subWeb, sourceContentType, updateFields, removeFields);
- subWeb.Dispose();
- }
- }
- static void Main(string[] args)
- {
- string siteAddress = "http://localhost";
- using (SPSite site = new SPSite(siteAddress))
- {
- using (SPWeb currentWeb = site.OpenWeb())
- {
- SPContentType pageCommentContentType = currentWeb.ContentTypes["Page Comment"];
- if (pageCommentContentType != null) // We have a content type
- {
- PropogateContentTypeChangesOnWeb(currentWeb, pageCommentContentType, true, false);
- }
- else // No content type is found.
- {
- Console.WriteLine("The content type does not exist in this site collection.");
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement