In this blog, we will see commonly required functions when we are working with C# and SharePoint.
Suppose, we have one SharePoint list and in that, we have Lookup column which allows selecting multiple values.
We need to get other fields of lookup list to perform different actions based on our requirement. So, below is the code to get multiple values in SPFieldLookupValueCollection type and based on this we can fetch other columns of the sub list.
Suppose, we have one SharePoint list and in that, we have Lookup column which allows selecting multiple values.
We need to get other fields of lookup list to perform different actions based on our requirement. So, below is the code to get multiple values in SPFieldLookupValueCollection type and based on this we can fetch other columns of the sub list.
- SPList lstMain = web.Lists.TryGetList("NameOfMainList");
- SPList lstSub = web.Lists.TryGetList("NameOfLookupList");
- SPListItem itemOfMainList = lstMain.GetItemById(ID);
- SPFieldLookupValueCollection values = new SPFieldLookupValueCollection(itemOfMainList["Name of lookup column"].ToString());
- foreach (SPFieldLookupValue value in values)
- {
- SPListItem itemOfSubList = lstSub.GetItemById(value.LookupId);
- string fieldName = value.LookupValue;
- if (itemOfSubList != null)
- {
- // Code to get another columns of look up list.
- string label = Convert.ToString(itemOfSubList["Column Name of sub/lookup list"]);
- }
- }
Below code is used to show JavaScript alert based on action performed. Window.location is used to redirect page to another page.
- ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Comment has been sucessfully added!!');window.location ='URl of another page.';", true);
Many times we fetched data from SharePoint list and want to set values to People editor field. So, for that I used the code given below.
- <SharePoint:PeopleEditor MultiSelect="false" SelectionSet="SPGroup,User" SharePointGroup="ITAssignee" ValidationEnabled="true" ID="cppAssignee" Width="500px" runat="server" VisibleSuggestions="3" Rows="1" CssClass="ms-long ms-spellcheck-true marginleft0" />
- setPeoplepickerData("cppAssignee", "ColumName", item);
- public void setPeoplepickerData(string fieldName, string columnName, SPListItem dataItem)
- {
- try
- {
- PeopleEditor pers = FindControl(fieldName) as PeopleEditor;
- ArrayList _arrayList = new ArrayList();
- SPFieldUser userField = (SPFieldUser)dataItem.Fields.GetField(columnName);
- if (!string.IsNullOrEmpty(Convert.ToString(dataItem[columnName])))
- {
- SPFieldUserValue userFieldValue = (SPFieldUserValue)userField.GetFieldValue(dataItem[columnName].ToString());
- PickerEntity _pickerEntity = new PickerEntity(); // PickerEntitiy use using Microsoft.SharePoint.WebControls
- _pickerEntity.Key = userFieldValue.User.LoginName;
- _pickerEntity.IsResolved = true;
- _arrayList.Add(_pickerEntity);
- }
- pers.UpdateEntities(_arrayList);
- }
- catch (Exception)
- {
- throw;
- }
- }

Join the conversation! Your thoughts help the community grow.