Thursday, 28 July 2016

Get List items in SharePoint Online Through CSOM

Friends, here is quick code snippet to get items of a list in SharePoint Online in CSOM from a Console Application.

Reference to Microsoft.SharePoint.Client.dll to be added.
Replace user name, password and site url with actuals from the code.
Code:

using Microsoft.SharePoint.Client;
using System;
using System.Security;
namespace TestConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string userName = "USERNAME";
            string password = "PASSWORD";
            string siteUrl = "SITEURL";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new SharePointOnlineCredentials(userName, GetPassword(password));
            Web web = clientContext.Web;
            List list = web.Lists.GetByTitle("Announcements");
            CamlQuery querry = CamlQuery.CreateAllItemsQuery(100);
            ListItemCollection items = list.GetItems(querry);
            clientContext.Load(items);
            clientContext.ExecuteQuery();
            foreach (var item in items)
            {
                Console.Write(item["Title"].ToString());
            }
            Console.Read();
        }
        private static SecureString GetPassword(string password)
        {
            if (password == null)
                throw new ArgumentNullException("password");
            var securePassword = new SecureString();
            foreach (char c in password)
                securePassword.AppendChar(c);
            securePassword.MakeReadOnly();
            return securePassword;
        }
    }
}

Creating and Updating List in SharePoint Online Through CSOM

Friends, here is quick code snippet to create and update lists in SharePoint Online in CSOM from a Console Application.

Reference to Microsoft.SharePoint.Client.dll to be added.
Replace user name, password and site url with actuals from the code.
Code:
 
 
using Microsoft.SharePoint.Client;
using System;
using System.Security;
namespace TestConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string userName = "USERNAME";
            string password = "PASSWORD";
            string siteUrl = "SITEURL";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new SharePointOnlineCredentials(userName, GetPassword(password));
            Web web = clientContext.Web;
            ListCreationInformation listCrInfo = new ListCreationInformation();
            listCrInfo.Title = "NewAnnouncements";
            listCrInfo.TemplateType = (int)ListTemplateType.Announcements;
            List list = web.Lists.Add(listCrInfo);
            list.Description = "New Announcements Description";
            list.Update();
            clientContext.ExecuteQuery();
        }
        private static SecureString GetPassword(string password)
        {
            if (password == null)
                throw new ArgumentNullException("password");
            var securePassword = new SecureString();
            foreach (char c in password)
                securePassword.AppendChar(c);
            securePassword.MakeReadOnly();
            return securePassword;
        }
    }
}

Wednesday, 27 July 2016

Creating Sub Site in SharePoint Online Through CSOM

Friends, here is quick code snippet to create sub site in SharePoint Online in CSOM from a Console Application.

Reference to Microsoft.SharePoint.Client.dll to be added.
Replace user name, password and site url with actuals from the code.
Code:



using Microsoft.SharePoint.Client;
using System;
using System.Security;

namespace TestConsoleApp 

                                                                  

class Program 


static void Main(string[] args)


string userName = "USERNAME";

string password = "PASSWORD";

string siteUrl = "SITEURL"

ClientContext clientContext = new ClientContext(siteUrl);

clientContext.Credentials = new SharePointOnlineCredentials(userName, GetPassword(password));

WebCreationInformation creation = new WebCreationInformation();

creation.Url = "NewWeb";

creation.Title = "NewWeb";

Web newWeb = clientContext.Web.Webs.Add(creation); 

clientContext.Load(newWeb, w => w.Title);

clientContext.ExecuteQuery();


private static SecureString GetPassword(string password)

 


if (password == null)

throw new ArgumentNullException("password");

var securePassword = new SecureString();

foreach (char c in password) 

securePassword.AppendChar(c);

securePassword.MakeReadOnly(); 

return securePassword; 

}

}     

}

 









 
 
 


 

Wednesday, 24 July 2013

Get Todays Date Using Java Script

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = mm+'-'+dd+'-'+yyyy;
alert(today);
 

Wednesday, 10 October 2012

Absolute Url Of List, List Item in SharePoint List or Library.


Some SharePoint objects expose relative paths.
Relative paths can be converted to full Url by adding current server, site paths.
To convert a relative path to the full URL, you add the current server,

Rather than performing path manipulation, "GetFullUrl" method can be used.

Example- Get Full Url Of A SharePoint List

SPUtility.GetFullUrl(SPContext.Current.Site, listobj.DefaultDisplayFormUrl);

Example- Get Full Url Of A SharePoint ListItem

SPUtility.GetFullUrl(SPContext.Current.Site,"/"+ itemobj.Url);