1: using System;
2: using System.Runtime.InteropServices;
3: using System.Security.Permissions;
4: using Microsoft.SharePoint;
5: using Microsoft.SharePoint.Security;
6: using System.Collections;
7:
8: namespace Your Namespace
9: {
10: /// <summary>
11: /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
12: /// </summary>
13: /// <remarks>
14: /// The GUID attached to this class may be used during packaging and should not be modified.
15: /// </remarks>
16:
17: [Guid("ebecc7c3-5d91-4bcf-a7d6-a332c786431e")]
18: public class YourClass_TypeEventReceiver : SPFeatureReceiver
19: {
20: // add the groupname for your content types and fields
21:
22: const string columnGroup = "GroupName";
23:
24: const string ctName = "ContentType";
25: // Uncomment the method below to handle the event raised after a feature has been activated.
26:
27: public override void FeatureActivated(SPFeatureReceiverProperties properties)
28: {
29: using (SPWeb spWeb = properties.GetWeb() as SPWeb)
30: {
31: //add the fields
32: addFields(spWeb);
33:
34: SPContentType testCT = spWeb.ContentTypes[ctName];
35: // we will not create the content type if it exists
36: //you could use an else clause here to update the content type if
37: //it exists
38: if (testCT == null)
39: {
40: //the content type does not exist add it
41: addContentType(spWeb, ctName);
42: //create the list if the content type dosen't exist
43: CreateAlertList(spWeb);
44: }
45:
46: }
47:
48: }
49:
50:
51:
52:
53: public void addFields(SPWeb spWeb)
54: {
55:
56: Utilities.addField(spWeb, "AlertBody", SPFieldType.Note, true, columnGroup);
57: Utilities.addField(spWeb, "AlertDateTimeStart", SPFieldType.DateTime, false, columnGroup);
58: Utilities.addField(spWeb, "AlertDateTimeEnd", SPFieldType.DateTime, false, columnGroup);
59:
60:
61: }
62:
63:
64: private static void addContentType(SPWeb spWeb, string name)
65: {
66: SPContentType myContentType = new SPContentType(spWeb.ContentTypes["Item"], spWeb.ContentTypes, name) { Group = columnGroup };
67:
68: spWeb.ContentTypes.Add(myContentType);
69: addContentTypeLinkages(spWeb, myContentType);
70:
71:
72: myContentType.Update();
73: }
74:
75: public static void addContentTypeLinkages(SPWeb spWeb, SPContentType ct)
76: {
77:
78: Utilities.addContentTypeLink(spWeb, "AlertBody", ct);
79: Utilities.addContentTypeLink(spWeb, "AlertDateTimeStart", ct);
80: Utilities.addContentTypeLink(spWeb, "AlertDateTimeEnd", ct);
81: }
82: private void CreateAlertList(SPWeb web)
83: {
84:
85: Guid newListGuid = web.Lists.Add("PriorityAlerts", " Corporate Alert List.",
86:
87: SPListTemplateType.GenericList);
88:
89: SPList newList = web.Lists[newListGuid];
90: newList.ContentTypesEnabled = true;
91: var news = web.ContentTypes[ctName];
92: newList.ContentTypes.Add(news);
93: newList.ContentTypes.Delete(newList.ContentTypes["Item"].Id);
94: newList.Update();
95: var view = newList.DefaultView;
96: //add all view fields here
97:
98:
99: view.ViewFields.Add("AlertBody");
100: view.ViewFields.Add("AlertDateTimeStart");
101: view.ViewFields.Add("AlertDateTimeEnd");
102: view.Update();
103:
104:
105:
106:
107: }
108:
109:
110:
111:
112: }
113: }
114: