Thursday, December 17, 2015

Authenticating .NET Client Object Model CSOM in Office 365 in SharePoint 2013

Authenticating .NET Client Object Model CSOM in Office 365 in SharePoint 2013
--------------------------------------------------------
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            const string webUrl = "https://sreekanth2.sharepoint.com/";
            const string USER = "sreekanth@sreekanth2.onmicrosoft.com";
            const string PWD = "password";
            var listInfo = ""; 
            using (ClientContext clientContext = new ClientContext(webUrl))
            {
                SecureString passWord = new SecureString();
                foreach (char c in PWD.ToCharArray())
                {
                    passWord.AppendChar(c);
                }
                clientContext.Credentials = new SharePointOnlineCredentials(USER, passWord);
                Microsoft.SharePoint.Client.List spList = clientContext.Web.Lists.GetByTitle("Temperature");
                clientContext.Load(spList);
                clientContext.ExecuteQuery();
                if (spList != null && spList.ItemCount > 0)
                {
                    Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml = @"<View><Query><Where><IsNotNull><FieldRef Name='ID' /></IsNotNull></Where><OrderBy><FieldRef Name='ID' /></OrderBy></Query><ViewFields><FieldRef Name='ID' /><FieldRef Name='City' /><FieldRef Name='Month' /><FieldRef Name='Temperature' /></ViewFields></View>";
                    SP.ListItemCollection listItems = spList.GetItems(camlQuery);
                    clientContext.Load(listItems);
                    clientContext.ExecuteQuery();
                    listInfo += "<table border='1'><tr><td>ID</td><td>City</td><td>Month</td><td>Temperature</td></tr>";
                    foreach (SP.ListItem oListItem in listItems)
                    {
                        listInfo += "<tr><td>" + oListItem.Id + "</td><td>" + oListItem["City"] + "</td><td>" + oListItem["Month"] + "</td><td>" + oListItem["Temperature"] + "</td></tr>";
                    }
                    listInfo += "</tr></table>";
                    div1.InnerHtml = "List Items found:<br/>" + listInfo;
                }
            }
        }
    }
}
--------------------------------------------------------
--------------------------------------------------------

No comments:

Post a Comment

Featured Post

Create SharePoint Folder Structure in Destination (Only If Not Exists)

Why This Script Is Safe You can run it multiple times It will not create duplicate folders It will only create missing folders S...

Popular posts