Search This Blog

Wednesday, June 22, 2016

How to add Items in sharepoint list using CSOM

public void AddNewItemToList(string Title,string Subject)
{
   using(ClientContext objCtx = new ClientContext("http://Sharepoint.com"))
   {
       List oList = ctx.Web.Lists.GetByTitle("TestList");
       ListItemCreationInformation objItemCreateInfo = new ListItemCreationInformation();
       ListItemnewItem = oList.AddItem(objItemCreateInfo);
       newItem["Title"] = Title;
       newItem["Subject"] = Subject;
       newItem.Update();
       context.ExecuteQuery();
    }
}

Wednesday, June 8, 2016

How to access the files in bin/debug within the project folder in Visual studio 2012?

string executableLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string xslLocation = Path.Combine(executableLocation, "doc.xml");

Retrieve Author of SPListItem as SPUser in SharePoint; programmatically (C#)

  public string GetListItemAuthorNameSPBuiltInFieldId(SPListItem spListItem)
      {
          string loginName = string.Empty;
          SPWeb spWeb = SPContext.Current.Web;

          var fullUserName = spListItem[SPBuiltInFieldId.Author] as string;
          var userName = fullUserName.Split('#')[1];//This is only to get the user name. My fullUserName was 1;#Setup Admin.
          SPUser spUser = spWeb.EnsureUser(userName);//You can get SPUser from here
          loginName = spUser.LoginName;
          return loginName;

      }

      public string GetListItemAuthorName(SPListItem spListItem)
      {
         string loginName = string.Empty;
         var spFieldUser = spListItem.Fields.GetFieldByInternalName("Author") as SPFieldUser;
   
         if (spFieldUser != null && spListItem["Author"] != null)
         {
            var fieldValue = spFieldUser.GetFieldValue(spListItem["Author"].ToString()) as SPFieldUserValue;
            if (fieldValue != null)
            {
               var spUser = fieldValue.User;//Get the SPUser
               loginName = spUser.LoginName;//Get the login name from SPUser
            }
         }
      return loginName;
      }

How to clear People Editor control value in sharepoint 2013 programmatically

<SharePoint:PeopleEditor id="spPPlEdtr" MultiSelect="true" 
  ValidatorEnabled="true" SelectionSet="User,SPGroup" runat="server"/>

Client Side:

<script type="text/javascript">

    // this registers clearPicker to run every time the page reloads

    _spBodyOnLoadFunctionNames.push("clearPicker");

     // this is the function that clears the picker and helper variables

    function clearPicker() {
        var control;
        var arr = document.getElementsByTagName("div");
        for (var i = 0; i < arr.length; i++) 
        {
            if (arr[i].id.indexOf("upLevelDiv") > 0)
            { control = arr[i]; }
        }
        control.innerHTML = '';
        arr = document.getElementsByTagName("input");
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].name.indexOf("hiddenSpanData") > 0)
            { control = arr[i]; }
        }
        control.value = '';
    }
</script>


Server Side:

PeopleEditor picker = spPPlEdtr; 
picker.Accounts.Clear(); 
picker.Entities.Clear(); 
picker.ResolvedEntities.Clear(); 

How to find strong name of an assembly using powershell

[System.Reflection.AssemblyName]::GetAssemblyName("dllpath").FullName