Geeks With Blogs

News

Hello,
My name is David Jacobus I am a former teacher of Computer Science and Mathematics, who is now working full time as a SharePoint Consultant! I have raced off-road motorcycles for 30+ years! I have fallen in love with Glamis and the Dune experience the last few years! My friends like to say: Jake, with age comes the cage! I suppose that is because at Glamis I use a Razor XP 900! Which has a full roll cage!


David Jacobus SharePoint Consultant

In the previous blog post I discussed working with the list import tool for creating lists which was a timesaver for developing lists in the UI and then importing that list into a Visual Studio solution. I had some time in-between projects to make this process for simple lists just using the default views. How about doing this entire process in about 15 minutes in code! This is such a time saver that I just had to blog about it and save other developers this time intensive task!

1. Use a utilities library for creating field’s and content types.

2. Create a feature receiver:

a. Create the fields

b. Create the Content Type

c. Create the list instance

My Utilities Library can be downloaded here

   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:  

I can now create lists instances quickly that it is now a trivial task!  Of course much of the work is contained in the Utilities Library.

Posted on Sunday, October 30, 2011 7:34 AM SharePoint | Back to top


Comments on this post: List Instance Woes Part 2 Win Win for all

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © David Jacobus | Powered by: GeeksWithBlogs.net