Simple SharePoint Code Reference


This is a short example doc I built for a certain vendor that was having issues with some event handler code covering safe calls for doing a lot of basic SharePoint functions. Posted here for reference :)

  1. using System;
  2. using System.Net;
  3. using System.Diagnostics;
  4. using System.Collections;
  5. using Microsoft.SharePoint;
  6. using Microsoft.SharePoint.Navigation;
  7. using Microsoft.SharePoint.Publishing;
  8. using Microsoft.SharePoint.Administration;
  9. using Microsoft.SharePoint.Taxonomy;
  10. using Microsoft.Office.Server.Diagnostics;
  11.  
  12. namespace AddMetadataField
  13. {
  14.     public class Util
  15.     {
  16.         //verify field does or does not already exist on list
  17.         internal bool ListFieldExists(SPList list, string fieldName)
  18.         {
  19.             return list.Fields.ContainsField(fieldName);
  20.         }
  21.        
  22.         //verify field does or does not already exist on spweb
  23.         internal bool WebFieldExists(SPWeb web, string fieldName)
  24.         {
  25.             return web.Fields.ContainsField(fieldName);
  26.         }
  27.        
  28.         //create field
  29.         internal SPField CreateField(string FieldName, SPFieldType SPType, SPWeb web, SPList list, bool Required = false, bool ListField = true, SPContentType AssociatedContentType = null)
  30.         {
  31.             web.AllowUnsafeUpdates = true;
  32.             SPField fld = null;
  33.             //add field to web if it’s not a list level field
  34.             if(!ListField)
  35.             {
  36.                 if(!WebFieldExists(web, FieldName)
  37.                 {
  38.                     fld = web.Fields.Add(FieldName, SPType, required);
  39.                     //disable triggering event handlers for this operation
  40.                     this.EventFiringEnabled = false;
  41.                     web.Update();
  42.                     this.EventFiringEnabled = true;
  43.                 }
  44.                 else
  45.                 {
  46.                     return web.Fields[FieldName];
  47.                 }
  48.             }
  49.             //create field on list
  50.             else
  51.             {
  52.                 if(AssociatedContentType == null)
  53.                 {
  54.                     if(!ListFieldExists(list, FieldName)
  55.                     {
  56.                         //add field to list if it’s not associated to any content type
  57.                         fld = list.Fields.Add(FieldName, SPType, required);
  58.                     }
  59.                     else
  60.                     {
  61.                         return list.Fields[FieldName];
  62.                     }
  63.                 }
  64.                 else
  65.                 {
  66.                     if(!AssociatedContentType.Fields.ContainsField(FieldName))
  67.                     {
  68.                         //add field to associated content type
  69.                         AssociatedContentType.Fields.Add(FieldName, SPType, required);
  70.                     }
  71.                     else
  72.                     {
  73.                         return AssociatedContentType.Fields[FieldName];
  74.                     }
  75.                 }
  76.                 //important to avoid event handler conflicts
  77.                 this.EventFiringEnabled = true;
  78.                 list.Update();
  79.                 this.EventFiringEnabled = false;
  80.             }
  81.             web.AllowUnsafeUpdates = false;
  82.             return fld;
  83.         }
  84.        
  85.         //create a new list item
  86.         internal SPListItem CreateItem(Hashtable Params, SPList list type string url = "/", SPContentType AssociatedContentType = null)
  87.         {
  88.             SPListItem result = list.AddItem();
  89.             foreach(DictionaryEntry prop in Params)
  90.             {
  91.                 result[prop.Key] = prop.Value;
  92.             }
  93.             //again need to disable event handler conflicts
  94.             this.EventFiringEnabled = false;
  95.             result.SystemUpdate();
  96.             this.EventFiringEnabled = true;
  97.             return result;
  98.         }
  99.     }
  100. }
  1. No comments yet.
(will not be published)


%d bloggers like this: