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.
- 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)
- {
-
- 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();
- _pickerEntity.Key = userFieldValue.User.LoginName;
- _pickerEntity.IsResolved = true;
- _arrayList.Add(_pickerEntity);
- }
- pers.UpdateEntities(_arrayList);
- }
- catch (Exception)
- {
- throw;
- }
- }
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.
- <script>
- $(document).ready(function() {
- $("#txtID").keypress(function(e) {
- if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
- alert("Only numbers allowed");
- return false;
- }
- });
- });
- </script>
- <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.
- <asp:Button ID="btnDownload" Text="Download" runat="server" OnClick="btnDownload_Click" OnClientClick="javascript:setFormSubmitToFalse()" CssClass="btn btn-primary" />
- function setFormSubmitToFalse()
- {
- setTimeout(function () { _spFormOnSubmitCalled = false; }, 1000);
- return true;
- }
I will also add some more functions in the next blog.
Thank you..