Commonly Required Functions/Snippet - Part Two

In this post, we will see the functions or the code snippets, which are very frequently required in SharePoint development.

To get value from people editor field, add control in .ascx file, as below.
  1. <SharePoint:PeopleEditor SelectionSet="User" AllowTypeIn="true" ValidationEnabled="true" ID="cppAuthor" Width="300px" runat="server" VisibleSuggestions="3" Rows="1" CssClass="ms-long ms-spellcheck-true marginleft0" />  
Write the below code in .cs file. We will get the username from people editor field in the string authorName.
  1. if (cppAuthor != null) {  
  2.     SPFieldUserValue persVal = GetPeopleFromPickerControl(cppAuthor, web);  
  3.     authorName = string.IsNullOrEmpty(Convert.ToString(persVal)) ? string.Empty : Convert.ToString(persVal).Split('#')[1];  
  4. }  
  5. public static SPFieldUserValue GetPeopleFromPickerControl(PeopleEditor people, SPWeb web) {  
  6.     SPFieldUserValue value = null;  
  7.     if (people.ResolvedEntities.Count > 0) {  
  8.         for (int i = 0; i < people.ResolvedEntities.Count; i++) {  
  9.             try {  
  10.                 PickerEntity user = (PickerEntity) people.ResolvedEntities[i];  
  11.                 switch ((string) user.EntityData["PrincipalType"]) {  
  12.                     case "User":  
  13.                         SPUser webUser = web.EnsureUser(user.Key);  
  14.                         value = new SPFieldUserValue(web, webUser.ID, webUser.Name);  
  15.                         break;  
  16.                     case "SharePointGroup":  
  17.                         SPGroup siteGroup = web.SiteGroups[user.EntityData["AccountName"].ToString()];  
  18.                         value = new SPFieldUserValue(web, siteGroup.ID, siteGroup.Name);  
  19.                         break;  
  20.                     default:  
  21.                         SPUser spUser = web.EnsureUser(people.Accounts[i].ToString());  
  22.                         value = new SPFieldUserValue(web, spUser.ID, spUser.Name);  
  23.                         break;  
  24.                 }  
  25.             } catch (Exception ex) {  
  26.                 SPUser spUser = web.EnsureUser(people.Accounts[i].ToString());  
  27.                 value = new SPFieldUserValue(web, spUser.ID, spUser.Name);  
  28.             }  
  29.         }  
  30.     }  
  31.     return value;  
  32. }  
Get parameter value from URL using JavaScript.
  1. var tech = getUrlParameter('requestID');  
  2. var getUrlParameter = function getUrlParameter(sParam) {  
  3.     var sPageURL = decodeURIComponent(window.location.search.substring(1)),  
  4.         sURLVariables = sPageURL.split('&'),  
  5.         sParameterName,  
  6.         i;  
  7.     for (i = 0; i < sURLVariables.length; i++) {  
  8.         sParameterName = sURLVariables[i].split('=');  
  9.         if (sParameterName[0] === sParam) {  
  10.             return sParameterName[1] === undefined ? true : sParameterName[1];  
  11.         }  
  12.     }  
  13. };  
The below function is used to clear textbox and select controls from specific div.
  1. function clearControl(section) {  
  2.     $('#' + section).find('input:text').val('');  
  3.     $('#' + section).find('select').val('');  
  4. }  
In the above function, "section" is the id of div.

Get value from the look up field.
  1. public string getLookupValue(string colName, SPListItem item) {  
  2.     string strvalue = string.Empty;  
  3.     try {  
  4.         if (!string.IsNullOrEmpty(Convert.ToString(item[colName]))) {  
  5.             SPFieldLookupValue fieldLookupValue = new SPFieldLookupValue(item[colName].ToString());  
  6.             strvalue = fieldLookupValue.LookupValue;  
  7.         }  
  8.     } catch (Exception) {  
  9.         throw;  
  10.     }  
  11.     return strvalue;  
  12. }  
Get item attachments and bind to div using string builder.
  1. <div id="fileAttachments" runat="server"> </div> if (item.Attachments.Count > 0) { SPAttachmentCollection attachColl = item.Attachments; string FileNames = String.Empty; StringBuilder filebuilder = new StringBuilder(); filebuilder.Append("  
  2. <div class='form-group marginBottom'>  
  3.     <div class='col-sm-3'><label class='fright' for='Attachments'>Attachments:</label></div>"); filebuilder.Append("  
  4.     <div class='col-sm-9'>  
  5.         <ul>"); for (int filenumbers = 0; filenumbers  
  6.             < item.Attachments.Count; filenumbers++) { SPFile file=i tem.ParentList.ParentWeb.GetFile(item.Attachments.UrlPrefix + item.Attachments[filenumbers].ToString()); filebuilder.Append( "<li><a href='" + item.Attachments.UrlPrefix + item.Attachments[filenumbers].ToString() + "'>" + file.Name + "</a></li>"); } filebuilder.Append( "</ul></div></div>"); fileAttachments.InnerHtml=f ilebuilder.ToString();  
  7. }