GSCustomMenuSection

Contents for SharePoint, ReactJS, C#, ASP .Net, JavaScript, JQuery, SQL

Friday, December 18, 2015

Create OR Update List item using CSOM C#

In this blog we will see how to create item/Update a list item using CSOM

Add two references to your visual studio solution
Microsoft.SharePoint.Client.dll &
Microsoft.SharePoint.Client.Runtime.dll


string webUri = "http://servername/sites/demo";
using (ClientContext context = new ClientContext(webUri))
{
    //By Default it will use windows credentials
    //If want to specify credentials use below line
    context.Credentials = new NetworkCredential(
                             "username",
                             "password",
                             "domain");

    Web web = context.Web;

    //Load the web object 
    context.Load(web); 
    List list = web.Lists.GetByTitle("MyList"); 

    //---------------Create a new Item-----------------//
    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
    ListItem newListItem = targetList.AddItem(itemCreateInfo);
    newListItem["Title"] = "My Item 1";
    newListItem["Comments"] = "Test item";
    newListItem.Update();

    context.Load(newListItem);


    //-----------------Set/ Update an item-----------//
    ListItem listItem = announcementsList.Items.GetById(1); 

    //Set new value to the Comments field.
    listItem["Comments"] = "Updated test value!!"; 
    listItem.Update(); 

    //---------------------------------------------------------//  
    //call Execute Query 
    //to get all the loaded object working
    context.ExecuteQuery();

    Console.WriteLine("Item created!  ID: " + newListItem.Id + "\nTitle: " + newListItem["Title"]);
}

Thursday, December 17, 2015

Connect to site from different environment/domain

In this blog we will see how to connecting to site from different environment using CSOM with login Credentials

Add two references to your visual studio solution
Microsoft.SharePoint.Client.dll &
Microsoft.SharePoint.Client.Runtime.dll


string webUri = "http://servername/sites/demo";
using (ClientContext context = new ClientContext(webUri))
{
        //By Default it will use windows credentials
        //If want to specify credentials use below line
        context.Credentials = new NetworkCredential(
                             "username",
                             "password",
                             "domain");

        Web web = context.Web;

        //Load the web object 
        context.Load(web); 

        //-----------------------------------------
        //Web rootWeb = context.Site.RootWeb;
        //context.Load(rootWeb);

        //call Execute Query 
        //to get all the loaded object working
        context.ExecuteQuery();

        Console.WriteLine(web.Title);
}

Wednesday, December 16, 2015

Verify current user access on current site

In this blog we will see how if current user having permissions a site


string webUri = //your site url;
using (ClientContext context = new ClientContext(webUri)) {  
    Web rootWeb = context.Site.RootWeb;
    context.Load(rootWeb);
    BasePermissions permission = new BasePermissions();
    permission.Set(PermissionKind.ManageWeb);
    ClientResult result= rootWeb.DoesUserHavePermissions(permission);
    try
    {                
        context.ExecuteQuery();
        if(result.Value){
            Console.WriteLine("You have access");
        }
        else{
     Console.WriteLine("You don't have access"); 
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("You don't have access");
    }
} 
Here basePermissions.Set(PermissionKind.ManageWeb); refers to access on web. Microsoft has provided more permissions levels we can verify.

Refer MSDN