1: using System;
2: using Microsoft.SharePoint;
3: using Microsoft.SharePoint.Publishing;
4:
5: namespace MB.SharePoint.ApplicationLogic
6: {
7: public class Utils
8: {
9: private static object _lock = new object();
10: private static string sourceVariationUrl = "";
11:
12:
13:
14: #region Columns and Content Types
15: public static void AddSiteColumn(SPSite site, string fieldTypeName, string fieldName, bool hidden, bool required, string description, string group, string defaultValue)
16: {
17: SPWeb web = site.RootWeb;
18:
19: if (!web.Fields.ContainsField(fieldName))
20: {
21: //add the field if it doesn't exists
22: SPField newField = web.Fields.CreateNewField(fieldTypeName, fieldName) as SPField;
23: newField.Hidden = hidden;
24: newField.Required = required;
25: newField.Description = description;
26: newField.Group = group;
27: if (newField != null)
28: newField.DefaultValue = defaultValue;
29: web.Fields.Add(newField);
30: web.Update();
31: }
32: }
33:
34:
35: public static void AddSiteColumnToContentType(SPSite site, SPContentTypeId contentTypeID, string fieldTypeName, string fieldName, bool hidden, bool required, string description, string group, string defaultValue)
36: {
37: SPWeb web = site.RootWeb;
38: SPContentType contentType = web.ContentTypes[contentTypeID];
39:
40: if (contentType != null)
41: {
42: AddSiteColumn(site, fieldTypeName, fieldName, hidden, required, description, group, defaultValue);
43:
44: if (contentType.FieldLinks[fieldName] == null)
45: {
46: //link the field to the content type
47: contentType.FieldLinks.Add(new SPFieldLink(web.Fields[fieldName]));
48: contentType.Update(true, false);
49: }
50: }
51:
52: }
53:
54: #endregion
55:
56:
57:
58: #region Publishing
59: public static bool isSourceVariation(string url, SPWeb web)
60: {
61: string sourceUrl = sourceVariationUrl;
62: if (sourceUrl == null || sourceUrl.Length == 0)
63: {
64: lock (_lock)
65: {
66: sourceUrl = sourceVariationUrl;
67: if (sourceUrl == null || sourceUrl.Length == 0)
68: {
69: //Get Source variation from list -- we cannot use API since we do not have a context
70: using (SPWeb rootWeb = web.Site.RootWeb)
71: {
72: try
73: {
74: //get labels
75: SPList labelsList = rootWeb.GetList("/variation labels");
76: SPQuery query = new SPQuery();
77:
78: query.Query = "<Where><Eq><FieldRef Name='Is_x0020_Source'/><Value Type='Boolean'>1</Value></Eq></Where>";
79: SPListItemCollection labels = labelsList.GetItems(query);
80: foreach (SPListItem item in labels)
81: {
82: sourceUrl = rootWeb.ServerRelativeUrl.ToUpper() + ((string)item["Title"]).ToUpper();
83: sourceVariationUrl = sourceUrl;
84: break;
85: }
86: }
87: catch (Exception)
88: {
89: //problem reading variation list, return false by default
90: return false;
91: }
92: }
93: }
94: }
95: }
96:
97: return url.ToUpper().StartsWith(sourceUrl);
98: }
99:
100:
101: public static void DeleteFieldFromAllPagesLists(SPWeb web, string fieldName)
102: {
103: if (PublishingWeb.IsPublishingWeb(web))
104: {
105: PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
106: SPList pages = pubWeb.PagesList;
107: if (pages.Fields.ContainsField(Consts.Fields.variationOnCreateOnly))
108: {
109: pages.Fields.Delete(Consts.Fields.variationOnCreateOnly);
110: pages.Update();
111: }
112:
113: pubWeb.Close();
114: }
115:
116:
117: if (web.Webs != null)
118: {
119: foreach (SPWeb subWeb in web.Webs)
120: {
121: DeleteFieldFromAllPagesLists(subWeb, fieldName);
122: subWeb.Dispose();
123: }
124: }
125: }
126:
127: #endregion
128:
129:
130:
131: #region EventReceivers
132: public static void AddEventReceiver(SPList list, string className, string assemblyName, SPEventReceiverType type, int sequenceNumber)
133: {
134:
135: bool isNewEvent = true;
136: foreach (SPEventReceiverDefinition eventReceiver in list.EventReceivers)
137: {
138: if (eventReceiver.Class.ToUpper().Equals(className) && eventReceiver.Type == type)
139: {
140: isNewEvent = false;
141: break;
142: }
143: }
144:
145:
146: //Add Event Receiver
147: if (isNewEvent)
148: {
149: SPEventReceiverDefinition eventReceiver = list.EventReceivers.Add();
150: eventReceiver.Type = type;
151: eventReceiver.Assembly = assemblyName;
152: eventReceiver.Class = className;
153: eventReceiver.SequenceNumber = sequenceNumber;
154: eventReceiver.Update();
155: }
156: }
157:
158: public static void DeleteEventReceiver(SPList list, string className, SPEventReceiverType type)
159: {
160: foreach (SPEventReceiverDefinition eventReceiver in list.EventReceivers)
161: {
162: if (eventReceiver.Class.ToUpper().Equals(className.ToUpper()) && eventReceiver.Type == type)
163: {
164: eventReceiver.Delete();
165: break;
166: }
167: }
168: }
169:
170: public static bool IsItemUpdatingFromCheckin(SPItemEventProperties properties)
171: {
172: return (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null && properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] != null);
173: }
174: #endregion
175:
176:
177:
178: #region Features
179: public static void ActivateFeatureRecursively(SPWeb web, Guid featureId, bool force, bool checkForPublishing)
180: {
181: if ((checkForPublishing && PublishingWeb.IsPublishingWeb(web)) || !checkForPublishing)
182: web.Features.Add(featureId, force);
183:
184: foreach (SPWeb subWeb in web.Webs)
185: {
186: ActivateFeatureRecursively(subWeb, featureId, force, checkForPublishing);
187: subWeb.Dispose();
188: }
189: }
190:
191: public static void DeactivateFeatureRecursively(SPWeb web, Guid featureId, bool force, bool checkForPublishing)
192: {
193: if (((checkForPublishing && PublishingWeb.IsPublishingWeb(web)) || !checkForPublishing) && web.Features[featureId] != null)
194: web.Features.Remove(featureId, force);
195:
196: foreach (SPWeb subWeb in web.Webs)
197: {
198: DeactivateFeatureRecursively(subWeb, featureId, force, checkForPublishing);
199: subWeb.Dispose();
200: }
201: }
202: #endregion
203:
204:
205: #region General
206:
207: public static bool IsInEditMode()
208: {
209: return SPContext.Current.FormContext.FormMode == Microsoft.SharePoint.WebControls.SPControlMode.Edit || SPContext.Current.FormContext.FormMode == Microsoft.SharePoint.WebControls.SPControlMode.New;
210: }
211:
212: #endregion
213: }
214: }