Wednesday, 19 November 2014

Add values to the different types of Fields in the SharePoint List Pragramatically - C#


In SharePoint list we have different types of fields.

While we insert data into the field, use field names are internal name of the field, don't use display name.

In the following snippets show how to add values to different types of fields in the SharePoint list:

Field Type - Single Line Text :

    listItem["ColumnName1"] = "value1";    // here "value" is the  data into the field.

Field Type - Multiple Line Text :

    listItem["ColumnName2"] = "value2";

Field Type - Boolean:

    listItem["ColumnName"] = "Yes" / true / "No" / false ;    //insert one value only :)


Field Type - Choice:

    listItem["ColumnName3"] = list.Fields["ColumnName3"].GetFieldValue(myChoice);

Field Type - Number:

    listItem["ColumnName4"] = value4;

Field Type - Currency:

   listItem["ColumnName5"] = "value5";

Field Type - Date and Time:

   listItem["ColumnName6"] = "DateTimeValue";


Field Type - LookUp:

  var department = web.Lists["Department"].GetItemById(1); // Get the Reference record.

  var  employee = web.Lists["Employee"].Items.Add();

  empolyee["Department"] = new SPFieldLookupValue(department.ID, department.Title);
                                              // Set the lookUp value to the field.
  employee.Update();

(Or) Simply we can set as follows:
  
      employee["Department"] = "1";

Field Type - Person or Group:

   SPUser user = null;

   user = web.EnsureUser("Domain\Loginname");

   listitem["manager"] = user;

Field Type - Hyper link :

   SPFieldUrlValue hyperlink = new SPFiedlUrlValue();

   hyperlink.Description = "Sharepoint Light";

   hyperlink.Url = "http://sharepointbabu.blogspot.in/";

   listitem["HyperLink"] = hyperlink;

   (Or) Simply we can set as follows:

    listitem["HyperLink"] = "http://sharepointbabu.blogspot.in/, Sharepoint Light";

 

Field Type - Calculated :

   Calculated field value is set automatically based on the given formula based on the other fields data.


In the following code snippet, insert data into different SharePoint list fields using C#.

---------------------------------------------------------------------------------------------------------------------

using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
  using (SPWeb web = site.OpenWeb())
   {
      web.AllowUnsafeUpdates = true;

          SPUser manager = null;

              try
                {
                  SPList list = web.Lists.TryGetList("employe");

                   SPUser user = null;    // for people picker insertion

                   user = web.EnsureUser("Domain\Loginname");  // for people picker insertion

                  if (list == null)
                     throw new Exception(string.Format("List {0} can not be found", Constants.List_ADUserAccounts));

                  SPListItem listitem = list.Items.Add();
   
                   listItem["SinglelinetextColumn"] = "value";

                   listItem["MultilinetextColumn"] = "value";

                   listItem["BooleanColumn"] = true ;

                   listItem["ChoiceColumn"] = list.Fields["ChoiceColumn"].GetFieldValue(myChoice);

                   listItem["NumberColumn"] = value;

                   listItem["CurrencyColumn"] = "value";

                   listItem["DateTimeColumn"] = "DateTimeValue";

                   listitem["managerColumn"] = user;

                   listitem["HyperLinkColumn"] = "http://nagababua.blogspot.in/, Fix Your Code";

                   listitem.Update();

                }

            catch
            {
                 throw;
             }

           finally
           {
                 web.AllowUnsafeUpdates = false;
           }
      }
}

-----------------------------------------------------------------------------------------------------------------------


  



No comments:

Post a Comment