Code Examples: Working With SharePoint Columns

How to manipulate columns in SharePoint.

Date Entered: 05/19/2011    Last Updated: 06/28/2011

The SharePoint Integrator gives users an easy way to manipulate columns on a SharePoint site.


Adding a New Column

To add a new column to a Document Library, use the following code:

 SPColumn newCol = new SPColumn();
 newCol.DisplayName = "NewColumnName";
 newCol.PrimaryKey = false;
 newCol.Required = false;
 newCol.UpdateOp = ColumnUpdateOps.cmoAdd;
 site.Columns.Add(newCol);
 site.UpdateListColumns("DocumentLibraryName");

The variable 'site' is an instance of the Spsite component that has been set with the necessary login credentials. The above code does the following:

  • It creates a new instance of SPColumn.
  • It sets the new column's name to "NewColumnName"
  • It states that the column is not a Primary Key.
  • It states that the column is not required.
  • It sets the update operation to add a new column.
  • It adds the new column to the Document Library "DocumentLibraryName".

Updating a Column for an Existing File

To update a column for an existing file, use the following code:

 list.Items.Clear();
 list.Items.Add(new SPItem());
 list.Items[0].FieldCount = 3;

 list.Items[0].FieldIndex = 0;
 list.Items[0].FieldName = "ID";
 list.Items[0].FieldValue = "2";

 list.Items[0].FieldIndex = 1;
 list.Items[0].FieldName = "FileRef";
 list.Items[0].FieldValue = /Path/To/Library/file.ext";

 list.Items[0].FieldIndex = 2;
 list.Items[0].FieldName = "FieldName";
 list.Items[0].FieldValue = "A new Field value";
 list.UpdateItems();

The variable 'list' is an instance of the Splist component that has been set to point to the Document Library where the file whose column entry you wish to update resides. This code does the following:

  • It clears the list of files from the component's Items list.
    • Doing this does not affect the actual files on the SharePoint.
  • It creates a new instance of SPItem and adds it to the list's Items list.
  • It sets the FieldCount of the item to 3.
    • You will always need at least 3 fields. The FileRef and ID fields are used by the Splist component to make sure the proper file is updated.
    • The example code is updating only one field, but you can update more by adding more fields with the FieldCount property.
  • It sets the first Field (index 0) as the ID with value 2.
    • The ID value must match the file's existing ID value from the SharePoint.
  • It sets the second Field (index 1) as the FileRef of the file to be updated.
    • The FileRef is the absolute path to the file on the SharePoint.
  • It sets the third Field (index 2) as the column you wish to update.
    • FieldName is the name of the column.
    • FieldValue is the value you wish to set.
  • It calls the UpdateItems() method to update the file information on the SharePoint.

Please Note that the columns must already exist to be set in this manner. If they do not exist yet, you will need to utilize the process outlined above to add new columns to a Document Library.

Uploading a File and Setting Column Data:

To upload a new file and set its column data at the same time, use the following code:

 docs.LocalFile = "C:\\Path\\To\\File\\file.ext";
 docs.Config("DocMetaInfo=Column1Name;Column1Value;Column2Name;Column2Value;...");
 docs.DocumentLibrary = "DocumentLibraryName";
 docs.Add("FileName.ext");

The variable 'docs' is an instance of the Spdocuments component. The above code does the following:

  • It sets the path to the file that is being uploaded.
  • It sets the DocMetaInfo configuration option to set the columns that are to be set and their values.
  • It sets the Document Library the file is to be uploaded to as "DocumentLibraryName"
  • It uploads the document.

Please Note that the columns must already exist to be set in this manner. If they do not exist yet, you will need to utilize the process outlined above to add new columns to a Document Library.

Retrieving Data From Custom Columns:

The easiest way to retrieve information from custom columns is by setting the IncludedField configuration option to include the custom columns and then utilizing the GetItemField method. An example can be found below.
list.Config("IncludedFields=Cust_Col_1,Cust_Col_2");
list.ListItems();

Console.WriteLine(list.GetItemField(0,"ows_Cust_Col_1"));

The variable 'list' is an instance of the Splist component that has been set to point to the Document Library where the file whose column entry you wish to read resides. This code does the following:

  • It sets the IncludedFields configuration option to include the custom columns Cust_Col_1 and Cust_Col_2.
  • It calls ListItems to load the Items property.
  • It writes the value of the Cust_Col_1 column of the first entry in the Items property to the console. Please note that the custom column name must be prefixed with 'ows_'.

We appreciate your feedback.  If you have any questions, comments, or suggestions about this entry please contact our support team at kb@nsoftware.com.