SharePoint List Helper class

12/05/2015 11:53

This class is used for common list item operation, get or set the value to/from list item safely

- Create ListHelper.cs class in your project

- copy and paste following class definition

- this class uses Logging.cs file, but if you have your own logger, just update appropriate method call

 

 

using Microsoft.SharePoint;

namespace MyProject.Codebehind
{
    public class ListHelper
    {
        private static string codeFileName = "ListHelper.cs";
        public static void SetValueToListItem(SPListItem item, string fieldStaticName, object value)
        {
            if (item != null)
            {
                if (TryGetFieldFromItem(item, fieldStaticName) != null)
                {
                    if (value != null)
                    {
                        item[fieldStaticName] = value;
                        item.Update();
                    }
                }
            }
        }

 

        public static object GetPropertyFromField(SPListItem item, string fieldName)
        {
            object propertyValue = null;
            if (item != null && !string.IsNullOrEmpty(fieldName))
            {
                if (TryGetFieldFromItem(item, fieldName) != null && item[fieldName] != null)
                {
                    propertyValue = item[fieldName];
                }
            }
            return propertyValue;
        }

 


        public static string GetStringPropertyFromField(SPListItem item, string fieldName)
        {
            string stringProperty = GetPropertyFromField(item, fieldName) != null ? GetPropertyFromField(item, fieldName).ToString() : string.Empty;
            return stringProperty;
        }

 


        public static SPField TryGetFieldFromItem(SPListItem item, string fieldStaticName)
        {
            SPField existingField = null;
            if (item != null)
            {
                if (item.Fields.TryGetFieldByStaticName(fieldStaticName) != null)
                {
                    existingField = item.Fields.TryGetFieldByStaticName(fieldStaticName);
                }
                else
                {
                    Logging.LogInformation(codeFileName, "TryGetFieldFromItem", string.Format("Field '{0}' not found in the list '{1}'", fieldStaticName, item.ParentList.Title));
                }
            }
            return existingField;
        }

 


        public static void SetStringPropertyToField(SPListItem item, string fieldName, string propertyValue)
        {
            SPField field = TryGetFieldFromItem(item, fieldName);
            if (field != null)
            {
                item[fieldName] = propertyValue;
                item.Update();
            }
        }

 

 public static void SetWebPropertyFromTextbox(SPWeb web, InputFormTextBox tbox, string webPropertyName)
 {
  if (web.Properties.ContainsKey(webPropertyName))
  {
   web.Properties[webPropertyName] = tbox.Text;
   web.Properties.Update();
  }
  else
  {
   web.Properties.Add(webPropertyName, tbox.Text);
   web.Properties.Update();
  }
 }

 public static void LoadWebPropertyToTextbox(SPWeb web, InputFormTextBox tbox, string webPropertyName)
 {
  if (web.Properties.ContainsKey(webPropertyName))
  {
   tbox.Text = web.Properties[webPropertyName];
  }
 }

public static string GetWebProperty(SPWeb web, string webPropertyName)
{
 string webPropertyValue = string.Empty;
 if (web.Properties.ContainsKey(webPropertyName))
 {
  webPropertyValue = web.Properties[webPropertyName];
 }
 return webPropertyValue;
}

 public static void SetDefaultContentType(SPList list, string contenTypeName)
 {
  IList cTypes = new List();
  SPFolder rootFolder = list.RootFolder;
  cTypes = rootFolder.ContentTypeOrder;
  SPContentType cType = cTypes.SingleOrDefault(ct => ct.Name == contenTypeName);
  int ctIndex = cTypes.IndexOf(cType);
  cTypes.RemoveAt(ctIndex);
  cTypes.Insert(0, cType);
  rootFolder.UniqueContentTypeOrder = cTypes;
  rootFolder.Update();
 }

 public static SPUser GetUserFieldValueFromItem(SPListItem item, string userFieldStaticName)
        {
            string spUser = ListHelper.GetStringPropertyFromField(item, userFieldStaticName);
            if (!string.IsNullOrEmpty(spUser))
            {
                SPFieldUser userField = (SPFieldUser)item.Fields.GetField(userFieldStaticName);
                SPFieldUserValue userFieldValue = (SPFieldUserValue)userField.GetFieldValue(spUser);

                SPUser user = userFieldValue.User;

                return user;
            }
            else
            {
                return null;
            }
        }

//e. g. listUrl is '/Lists/Announcements' or library '/Shared Documents'
public static SPList GetListByUrl(SPWeb web, string listUrl)
{
 string listRelativeUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, listUrl);
 try
 {
  SPList list = web.GetList(listRelativeUrl);
  return list;
 }
 catch (SPException)
 {
  return null;
 }

    }
}