Sunday, February 22, 2015

Updating the SharePoint Online site logo with power shell code

In order to update the site logo for a Office 365 SharePoint site below code snippet can be used. I had used a SharePoint office 365 developer site to change the site logo.

$credential=Get-Credential
$username=$credential.UserName
$password=$credential.GetNetworkCredential().Password
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$url = "https://spk0365.sharepoint.com/sites/PradeepLearning"

 ### References # Specify the path where the dll's are located.
 Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
 Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

 $clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
 $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
 $clientContext.Credentials = $credentials
 #Get the SharePoint Web
 $web=$clientContext.Web;
 $web.SiteLogoUrl="https://spk0365.sharepoint.com/sites/PradeepLearning/SiteAssets/team-india-630-new-odi-kit.jpg";
 $web.Update();
 $clientContext.Load($web);
 $clientContext.ExecuteQuery();

Wednesday, February 4, 2015

Power Shell for SharePoint Office 365

For SharePoint 2013 on premises we had around 700+ command lets but SharePoint 2013 Online has a limited command-lets of 30 because of this limitation we are going to use power shell with Client side object model.Below are prerequisites for running a power shell script.

1. You need to have Power shell ISE
2. You need to have assemblies and these can be downloaded here

Power Shell scripts for retrieving all the lists present in the site and exporting it to CSV File

#Enter the credentials in dialog box
$credential = Get-Credential
$username=$credential.UserName
$password=$credential.GetNetworkCredential().Password
$securePassword=ConvertTo-SecureString $password -AsPlainText -Force

##URL for site
$url="https://spk0365.sharepoint.com/sites/PradeepLearning"

#Add dll
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

## Function to retrieve lists in the site
function GetAllLists()
{
$clientContext=New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials=New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$securePassword)
$clientContext.Credentials=$credentials
$web=$clientContext.Web;
$listColl=$web.Lists;
$clientContext.Load($listColl);

#Execute query will execute the query with server and will load site's lists properties in ClientContext
$clientContext.ExecuteQuery();
foreach($list in $listColl)
{
Write-Host "List Name: " $list.Title " ID: " $list.Id

#Writing the Lists properties to CSV
$web.Lists | select Title,ID | Export-CSV c:\Lists.csv
}
}

#Calling the function
GetAllLists

Monday, February 2, 2015

XSLT in SharePoint

XSLT stands for Extensible style sheet transformations. It is basically used to convert XML document into HTML output file. It can be used to present xml data in many different ways.
In SharePoint it is used as part of
  • XML Viewer Web Part
  • XSLT list view web part
  • Content query web part XSLT
  • Search results can be formatted using XSLT
These topic covers example of XML viewer web part in SharePoint 2013 Online. We will be using two files, one is xml file which contains xml data and other is xslt file which basically reads each node and transforms the data to html page. I had taken xml file from this link.
SharePoint Out of the box has list view webpart so we will be using this webpart and it bascially asks for two sources xml and xslt files. I had prepared xslt using the xml file and below is the code snippet for xslt. Once these files are configured properly we should be able to see the XMlL viewer webpart.


Saturday, January 24, 2015

Creating new user SharePoint group using CSOM for SharePoint 2013 Office 365

We can create SharePoint user group using CSOM for SharePoint 2013 in Visual Studio by using below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using System.Security;
namespace GetListCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext clientContext = new ClientContext("https://abc.sharepoint.com/");
            Web webUrl=clientContext.Web;
            Console.WriteLine("Enter your user name (ex: abc@microsoft.microsoftonline.com):");
            string userName = Console.ReadLine();
            Console.WriteLine("Enter your password.");
            SecureString password = GetPasswordFromConsoleInput();
            clientContext.Credentials = new SharePointOnlineCredentials(userName, password);

            GroupCollection grpcoll = webUrl.SiteGroups;
            GroupCreationInformation gpcreate = new GroupCreationInformation();
            gpcreate.Title = "TestUserGroup";
            gpcreate.Description = "TestGroup";
            Group newGroup = grpcoll.Add(gpcreate);
            clientContext.Load(newGroup);
            clientContext.ExecuteQuery();
            Console.WriteLine("New Group name is:" + gpcreate.Title);
            Console.ReadLine();

        }
        private static SecureString GetPasswordFromConsoleInput()
        {
            ConsoleKeyInfo info;

            //Get the user's password as a SecureString
            SecureString securePassword = new SecureString();
            do
            {
                info = Console.ReadKey(true);
                if (info.Key != ConsoleKey.Enter)
                {
                    securePassword.AppendChar(info.KeyChar);
                }
            }
            while (info.Key != ConsoleKey.Enter);
            return securePassword;
        }
    }
}
Creating a List View using Client Side Object Model for SharePoint 2013 Office 365

We can create a view using below code in visual studio by using CSOM while you execute the code it will ask for user id and password for your Office 365.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using System.Security;
namespace GetListCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext clientContext = new  ClientContext("https://abc.sharepoint.com");
            Web webUrl=clientContext.Web;
            Console.WriteLine("Enter your user name (ex: abc@microsoft.microsoftonline.com):");
            string userName = Console.ReadLine();
            Console.WriteLine("Enter your password.");
            SecureString password = GetPasswordFromConsoleInput();
            clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
            List list = webUrl.Lists.GetByTitle("doctest");
            ViewCollection viewcoll = list.Views;

            string[] viewFields = { "Title" };

            ViewCreationInformation creationInfo = new ViewCreationInformation();
            creationInfo.Title = "Public View";
            creationInfo.ViewFields = viewFields;
            creationInfo.RowLimit = 5;
            viewcoll.Add(creationInfo);
            clientContext.ExecuteQuery();
        }
        private static SecureString GetPasswordFromConsoleInput()
        {
            ConsoleKeyInfo info;

            //Get the user's password as a SecureString
            SecureString securePassword = new SecureString();
            do
            {
                info = Console.ReadKey(true);
                if (info.Key != ConsoleKey.Enter)
                {
                    securePassword.AppendChar(info.KeyChar);
                }
            }
            while (info.Key != ConsoleKey.Enter);
            return securePassword;
        }
    }
}

Tuesday, January 20, 2015

Accessing first 5 items in a list using CSOM for SharePoint 2013 Online

Below code snippet shows retrieving first 5 items in a list using CSOM for SharePoint 2013 Online by usning Visual Studio.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using System.Security;
namespace GetListCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext clientContext = new ClientContext("https://abc.sharepoint.com");
            Web webUrl=clientContext.Web;
            Console.WriteLine("Enter your user name (ex: abc@microsoft.microsoftonline.com):");
            string userName = Console.ReadLine();
            Console.WriteLine("Enter your password.");
            SecureString password = GetPasswordFromConsoleInput();
            clientContext.Credentials = new SharePointOnlineCredentials(userName, password);

            //Get the list by Title
            List list = webUrl.Lists.GetByTitle("doctest");

            // view fields
            string[] viewFields = { "Title" };

            //Camlquery to retrieve the items from the custom list
            CamlQuery query = CamlQuery.CreateAllItemsQuery(5, viewFields);

            //Get all the items from the list
            ListItemCollection itemcoll = list.GetItems(query);
            clientContext.Load(itemcoll);

            clientContext.ExecuteQuery();

            foreach (ListItem item in itemcoll)
            {
                Console.WriteLine(item["Title"].ToString());
            }
            Console.ReadLine();

        }
        private static SecureString GetPasswordFromConsoleInput()
        {
            ConsoleKeyInfo info;

            //Get the user's password as a SecureString
            SecureString securePassword = new SecureString();
            do
            {
                info = Console.ReadKey(true);
                if (info.Key != ConsoleKey.Enter)
                {
                    securePassword.AppendChar(info.KeyChar);
                }
            }
            while (info.Key != ConsoleKey.Enter);
            return securePassword;
        }
    }
}

Creating new item in the list using CSOM for SharePoint 2013 Online and retrieving the list items

If incase we happend to create a list item using CSOM for Sharepoint 2013 Online below code can be used.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using System.Security;
namespace GetListCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext clientContext = new ClientContext("https://abc.sharepoint.com/");
            Web webUrl=clientContext.Web;
            Console.WriteLine("Enter your user name (ex: abc@microsoft.microsoftonline.com):");
            string userName = Console.ReadLine();
            Console.WriteLine("Enter your password.");
            SecureString password = GetPasswordFromConsoleInput();
            clientContext.Credentials = new SharePointOnlineCredentials(userName, password);

            //Get the list by Title
            List list = webUrl.Lists.GetByTitle("doctest");

            //create a new item
            ListItemCreationInformation creationinfo = new ListItemCreationInformation();
            ListItem item = list.AddItem(creationinfo);

            //Set the title value for the new item
            item["Title"] = "New Item";

            //Update the item
            item.Update();
            clientContext.Load(item);

            //Execute the query to the server
            clientContext.ExecuteQuery();

            //Camlquery to retrieve the item from the custom list
            CamlQuery query = CamlQuery.CreateAllItemsQuery();

            //Get all the items from the list
            ListItemCollection itemcoll = list.GetItems(query);
            clientContext.Load(itemcoll);

            clientContext.ExecuteQuery();

            //Loop through all the items
            foreach (ListItem items in itemcoll)
            {
                //Display the item title field value
                Console.Write(items["Title"].ToString() + " \n ");

            }
            Console.ReadLine();

        }
        private static SecureString GetPasswordFromConsoleInput()
        {
            ConsoleKeyInfo info;

            //Get the user's password as a SecureString
            SecureString securePassword = new SecureString();
            do
            {
                info = Console.ReadKey(true);
                if (info.Key != ConsoleKey.Enter)
                {
                    securePassword.AppendChar(info.KeyChar);
                }
            }
            while (info.Key != ConsoleKey.Enter);
            return securePassword;
        }
    }
}


Retrieving list items using SharePoint 2013 CSOM Model

Below code can be used to retrieve the list items for a specific list. Below code should be compiled in visual studio.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using System.Security;
namespace GetListCollection
{
    class Program
    {
        static void Main(string[] args)
        {

            ClientContext clientContext = new ClientContext(https://abc.sharepoint.com");
            Web webUrl=clientContext.Web;
            Console.WriteLine("Enter your user name (ex: abc@microsoft.microsoftonline.com):");
            string userName = Console.ReadLine();
            Console.WriteLine("Enter your password.");
            SecureString password = GetPasswordFromConsoleInput();
            clientContext.Credentials = new SharePointOnlineCredentials(userName, password);

            //Get the list by Title
            List list = webUrl.Lists.GetByTitle("doctest");

            //Caml query to retrieve the items from custom list
            CamlQuery query = CamlQuery.CreateAllItemsQuery();

            //Get all the items from the list
            ListItemCollection itemcoll = list.GetItems(query);
            clientContext.Load(itemcoll);

            //Execute the query to the server
            clientContext.ExecuteQuery();

            //Loop through all the items
            foreach (ListItem item in itemcoll)
            {
                //Display the item title field value
                Console.WriteLine(item["Title"].ToString());
            }
            Console.ReadLine();
        }
        private static SecureString GetPasswordFromConsoleInput()
        {
            ConsoleKeyInfo info;

            //Get the user's password as a SecureString
            SecureString securePassword = new SecureString();
            do
            {
                info = Console.ReadKey(true);
                if (info.Key != ConsoleKey.Enter)
                {
                    securePassword.AppendChar(info.KeyChar);
                }
            }
            while (info.Key != ConsoleKey.Enter);
            return securePassword;
        }
    }
}
Creating a list using CSOM in SharePoint Office 365

Below code can be used to create a list in SharePoint Office 365 using CSOM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using System.Security;
namespace GetListCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext clientContext = new ClientContext("https://abc.sharepoint.com");
            Web webUrl=clientContext.Web;
            Console.WriteLine("Enter your user name (ex: abc@microsoft.microsoftonline.com):");
            string userName = Console.ReadLine();
            Console.WriteLine("Enter your password.");
            SecureString password = GetPasswordFromConsoleInput();
            clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
         
            //specifying properties of new custom list
            ListCreationInformation creationinfo=new ListCreationInformation();
            creationinfo.Title="Custom CSOM List";
            creationinfo.Description="Created using VS CSOM";
            creationinfo.TemplateType=(int)ListTemplateType.GenericList;

            //create a new custom list
            List newlist = clientContext.Web.Lists.Add(creationinfo);

            //Retrieve the custom list properties
            clientContext.Load(newlist);

            //executing the query to server
            clientContext.ExecuteQuery();

            //Display the properties of the list
            Console.WriteLine(newlist.Title);
            Console.ReadLine();
         
         
        }
         
     
        private static SecureString GetPasswordFromConsoleInput()
        {
            ConsoleKeyInfo info;

            //Get the user's password as a SecureString
            SecureString securePassword = new SecureString();
            do
            {
                info = Console.ReadKey(true);
                if (info.Key != ConsoleKey.Enter)
                {
                    securePassword.AppendChar(info.KeyChar);
                }
            }
            while (info.Key != ConsoleKey.Enter);
            return securePassword;
        }
    }
}
Connecting SharePoint 2013 Office 365 through CSOM

In order to connect to SharePoint Office 365 through CSOM you need to have following Dlls that need to be added to visual studio project. These dlls are:

Microsoft.SharePoint.Client
Microsoft.SharePoint.Client.Runtime.dll
Microsoft.SharePoint.Client. Taxonomy.dll
Microsoft.SharePoint.Client.UserProfiles.dll

Below code can be used to create a connection to SharePoint 2013 Office 365

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using System.Security;
namespace GetListCollection
{
    class Program
    {
        static void Main(string[] args)
        {           
                string webUrl = "https://abc.sharepoint.com";
                Console.WriteLine("Enter your user name (ex: john@mytenant.microsoftonline.com):");
                string userName = Console.ReadLine();
                Console.WriteLine("Enter your password.");               
                SecureString password = GetPasswordFromConsoleInput();
                using (var context = new ClientContext(webUrl))
                {
                    context.Credentials = new SharePointOnlineCredentials(userName, password);
                    context.Load(context.Web, w => w.Title);
                    context.ExecuteQuery();
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("Your site title is: " + context.Web.Title);
                    Console.ForegroundColor = defaultForeground;
                  
                }
           
        }
        private static SecureString GetPasswordFromConsoleInput()
        {
            ConsoleKeyInfo info;

            //Get the user's password as a SecureString
            SecureString securePassword = new SecureString();
            do
            {
                info = Console.ReadKey(true);
                if (info.Key != ConsoleKey.Enter)
                {
                    securePassword.AppendChar(info.KeyChar);
                }
            }
            while (info.Key != ConsoleKey.Enter);
            return securePassword;
        }
    }

}