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.
- <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.
- if (cppAuthor != null) {
- SPFieldUserValue persVal = GetPeopleFromPickerControl(cppAuthor, web);
- authorName = string.IsNullOrEmpty(Convert.ToString(persVal)) ? string.Empty : Convert.ToString(persVal).Split('#')[1];
- }
- public static SPFieldUserValue GetPeopleFromPickerControl(PeopleEditor people, SPWeb web) {
- SPFieldUserValue value = null;
- if (people.ResolvedEntities.Count > 0) {
- for (int i = 0; i < people.ResolvedEntities.Count; i++) {
- try {
- PickerEntity user = (PickerEntity) people.ResolvedEntities[i];
- switch ((string) user.EntityData["PrincipalType"]) {
- case "User":
- SPUser webUser = web.EnsureUser(user.Key);
- value = new SPFieldUserValue(web, webUser.ID, webUser.Name);
- break;
- case "SharePointGroup":
- SPGroup siteGroup = web.SiteGroups[user.EntityData["AccountName"].ToString()];
- value = new SPFieldUserValue(web, siteGroup.ID, siteGroup.Name);
- break;
- default:
- SPUser spUser = web.EnsureUser(people.Accounts[i].ToString());
- value = new SPFieldUserValue(web, spUser.ID, spUser.Name);
- break;
- }
- } catch (Exception ex) {
- SPUser spUser = web.EnsureUser(people.Accounts[i].ToString());
- value = new SPFieldUserValue(web, spUser.ID, spUser.Name);
- }
- }
- }
- return value;
- }
Get parameter value from URL using JavaScript.
- var tech = getUrlParameter('requestID');
- var getUrlParameter = function getUrlParameter(sParam) {
- var sPageURL = decodeURIComponent(window.location.search.substring(1)),
- sURLVariables = sPageURL.split('&'),
- sParameterName,
- i;
- for (i = 0; i < sURLVariables.length; i++) {
- sParameterName = sURLVariables[i].split('=');
- if (sParameterName[0] === sParam) {
- return sParameterName[1] === undefined ? true : sParameterName[1];
- }
- }
- };
The below function is used to clear textbox and select controls from specific div.
- function clearControl(section) {
- $('#' + section).find('input:text').val('');
- $('#' + section).find('select').val('');
- }
In the above function, "section" is the id of div.
Get value from the look up field.
- public string getLookupValue(string colName, SPListItem item) {
- string strvalue = string.Empty;
- try {
- if (!string.IsNullOrEmpty(Convert.ToString(item[colName]))) {
- SPFieldLookupValue fieldLookupValue = new SPFieldLookupValue(item[colName].ToString());
- strvalue = fieldLookupValue.LookupValue;
- }
- } catch (Exception) {
- throw;
- }
- return strvalue;
- }
Get item attachments and bind to div using string builder.
- <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("
- <div class='form-group marginBottom'>
- <div class='col-sm-3'><label class='fright' for='Attachments'>Attachments:</label></div>"); filebuilder.Append("
- <div class='col-sm-9'>
- <ul>"); for (int filenumbers = 0; filenumbers
- < 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();
- }