Commonly Required Functions/Snippet - Part Three

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.
  1. SPList lstMain = web.Lists.TryGetList("NameOfMainList");  
  2. SPList lstSub = web.Lists.TryGetList("NameOfLookupList");  
  3. SPListItem itemOfMainList = lstMain.GetItemById(ID);  
  4. SPFieldLookupValueCollection values = new SPFieldLookupValueCollection(itemOfMainList["Name of lookup column"].ToString());  
  5. foreach (SPFieldLookupValue value in values)  
  6. {  
  7.     SPListItem itemOfSubList = lstSub.GetItemById(value.LookupId);  
  8.     string fieldName = value.LookupValue;  
  9.     if (itemOfSubList != null)  
  10.     {  
  11.         // Code to get another columns of look up list.  
  12.         string label = Convert.ToString(itemOfSubList["Column Name of sub/lookup list"]);  
  13.     }  
  14. }  
Below code is used to show JavaScript alert based on action performed. Window.location is used to redirect page to another page.
  1. ScriptManager.RegisterStartupScript(thisthis.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.
  1. <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" />  
  2. setPeoplepickerData("cppAssignee""ColumName", item);  
  3. public void setPeoplepickerData(string fieldName, string columnName, SPListItem dataItem)  
  4. {  
  5. try  
  6. {  
  7.     PeopleEditor pers = FindControl(fieldName) as PeopleEditor;  
  8.     ArrayList _arrayList = new ArrayList();  
  9.     SPFieldUser userField = (SPFieldUser)dataItem.Fields.GetField(columnName);  
  10.     if (!string.IsNullOrEmpty(Convert.ToString(dataItem[columnName])))  
  11.     {  
  12.         SPFieldUserValue userFieldValue = (SPFieldUserValue)userField.GetFieldValue(dataItem[columnName].ToString());  
  13.         PickerEntity _pickerEntity = new PickerEntity(); // PickerEntitiy use using Microsoft.SharePoint.WebControls  
  14.         _pickerEntity.Key = userFieldValue.User.LoginName;  
  15.         _pickerEntity.IsResolved = true;  
  16.         _arrayList.Add(_pickerEntity);  
  17.     }  
  18.     pers.UpdateEntities(_arrayList);  
  19.     }  
  20.     catch (Exception)  
  21.     {  
  22.      throw;  
  23.     }  
  24. }  
Number Validation using jQuery. I have one text box with TextMode="Number" but it allows letter "e" even if Number type so to validate textbox only for Number values use below code.
  1. <script>  
  2.     $(document).ready(function() {  
  3.         $("#txtID").keypress(function(e) {  
  4.             if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {  
  5.                 alert("Only numbers allowed");  
  6.                 return false;  
  7.             }  
  8.         });  
  9.     });  
  10. </script>  
  11. <asp:TextBox ID="txtID" ClientIDMode="Static" class="form-control" TextMode="Number" runat="server"></asp:TextBox>  
It will show alert every time we type letters other than Number.

If we are downloading a certain file using Asp: button and after that button click page stops working, then write the code below in JavaScript file.

First, add event on asp:Button as below.
  1. <asp:Button ID="btnDownload" Text="Download" runat="server" OnClick="btnDownload_Click" OnClientClick="javascript:setFormSubmitToFalse()" CssClass="btn btn-primary" />  
  2. function setFormSubmitToFalse()  
  3. {  
  4.     setTimeout(function () { _spFormOnSubmitCalled = false; }, 1000);  
  5.     return true;  
  6. }  
I will also add some more functions in the next blog.

Thank you..