Sunday, September 29, 2013

SharePoint Utility Functions






Parse SharePoint SPField Single SPUser


1 /// <summary> 2 /// Parses and returns name from SPUserValue since the value is in the format 143;#John Doe 3 /// </summary> 4 /// <param name="value"></param> 5 /// <returns></returns> 6 public static string GetNameFromSingleSPUserField(string value) 7 { 8 if (string.IsNullOrEmpty(value)) return string.Empty; 9 10 if (value.Contains("#")) 11 return value.Substring(value.IndexOf('#') + 1, (value.Length - value.IndexOf('#')) - 1); 12 13 return value; 14 }




Get SharePoint SPFieldChoice Values

       
  • Print
  •    
  • About BlogTrog Code Window
 1 public static string[] GetSPChoiceFieldChoices(SPField spField)
 2         {
 3             System.Collections.Specialized.StringCollection choices = new System.Collections.Specialized.StringCollection();
 4 
 5             try
 6             {
 7                 if (spField is SPFieldChoice)
 8                 {
 9                     choices = ((SPFieldChoice)spField).Choices;
10                 }
11                 else if (spField is SPFieldMultiChoice)
12                 {
13                     choices = ((SPFieldMultiChoice)spField).Choices;
14                 }
15 
16                 if (choices != null && choices.Count > 0)
17                 {
18                     string[] values = new string[choices.Count];
19                     choices.CopyTo(values, 0);
20 
21                     return values;
22                 }
23             }
24             catch { }
25 
26             return new string[] { };
27         }