Blog of Sundar Narasiman

How to get Users and Groups in SharePoint 2013 Online using CSOM

In this post, we’ll see how we can retrieve users in a SharePoint group in SharePoint 2013 Online using managed .NET Client Side Object Model (CSOM). We’ll create a Console Application in Visual Studio for the purpose of the demo.

Open Visual Studio 2013.

File –> New Project –> Visual C# –> Console Application and name it as ‘GetUsersInGroup’

Add a reference to assemblies ‘Microsoft.SharePoint.Client’ and ‘Microsoft.SharePoint.Client.Runtime’.

Import the following two namespaces.

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

In the client object model, the list groups in a site-collection needs to be fetched first and then based on the groups another call needs to be made to SharePoint Online to get the list of users. The following code snippet explains it all.

namespace GetUsersInGroupCSOM
{

    class Program
    {
        static void Main(string[] args)
        {
            //Replace it with the url of your tenant or your site-collection
            string SiteUrl = "https://yoursite.sharepoint.com";

            System.Uri oUri = new System.Uri(SiteUrl);

            using (ClientContext oClientContext = new ClientContext(SiteUrl))
            {
                //Replace it with your user id for SharePoint Online
                string UserName = "lavsunswe@yoursite.onmicrosoft.com";


                //Replace it with your password
                string Password = "yourpassword";

                //Create a SecureString object from password string, needed for SharePointOnlineCredentials class
                SecureString SecurePassword = GetSecureString(Password);
                oClientContext.Credentials = new SharePointOnlineCredentials(UserName, SecurePassword);

                //Load the site-collection groups using CSOM
                oClientContext.Load(oClientContext.Web.SiteGroups);
                oClientContext.ExecuteQuery();

                GroupCollection oSiteCollectionGroups= oClientContext.Web.SiteGroups;
                Console.WriteLine("List of groups in the site collection");
                Console.WriteLine("-------------------------------------");

                foreach (Group  oGroup in oSiteCollectionGroups)

                {
                    
                    Console.WriteLine(oGroup.Title);
                    Console.WriteLine("n");


                                
                }

                //Load the users collection in the Group 1

                oClientContext.Load(oSiteCollectionGroups[1].Users);
                oClientContext.ExecuteQuery();

                Console.WriteLine("List of users in the first group of site-collection");
                Console.WriteLine("-------------------------------------");
              foreach(User oUser in oSiteCollectionGroups[1].Users)
                {
                    Console.WriteLine(oUser.Title);
                    Console.WriteLine("n");
                
                }


               
                Console.ReadLine();



            }



        }

        private static SecureString GetSecureString(String Password)
        {
            SecureString oSecurePassword = new SecureString();

            foreach (Char c in Password.ToCharArray())
            {
                oSecurePassword.AppendChar(c);

            }
            return oSecurePassword;
        }

    }
}

image

 Subscribe to my blog

Office 365 development patterns & practices released

The Office 365 Development Patterns & Practices team have shipped the new open source repository today to GitHub.com/OfficeDev. The whole idea of releasing this is that, many sharepoint developers were familiar with how to build custom solutions with full-trust code and VSTO, however they did not know how to implement these solutions using App Model. The set of samples addresses the transformation from Full Trust Code to App Model.

The additional content for Office 365 development is available in Microsoft Download Center SharePoint 2013 and SharePoint Online solution pack for branding and site provisioning.

 Subscribe to my blog

List of Azure services that can be moved from one subscription to another subscription

I have a requirement to transfer the entire SharePoint 2013 Farm on  Azure with Virtual Network, Cloud services, Storage accounts, Virtual Machines, Subnets and Availability sets to a different Subscription on Azure. I did some analysis and in this migration process i have learnt that the following Azure services (as of today 07/29/2014) can be transferred from one Azure subscription to another Azure subscription without data-loss and downtime, by raising a support ticket with Microsoft Azure Support :-

  • Virtual Machines
  • Cloud Services
  • CDN
  • Web sites
  • Media Services
  • Service Bus
  • Storage
  • Multi Factor Authentication
  • Traffic Manager
  • Mobile Services
  • Virtual Network
  • Access Control Service (ACS)
  • Caching – we need to work with Engineering Team for migrating caching service
  • Reserved IP Address and the Reserved IPs under the list

However, there are certain Azure services that cannot be moved from One subscription to another subscription (as of today 07/29/2014):-

a) Active Directory (AAD)
b) BizTalk Services
c) HD Insight
d) Backup 
e) Hyper-V Recovery Manager
f) Azure Store
g) Import / Export
h) Scheduler
i) Management Services 
j) SQL Reporting

The following pre-conditions should be met before initiating a subscription transfer with Microsoft Azure support :

  • Source and destination subscription should be active
  • Selective service transfer is not feasible as of today, it might come in future. It has to be all or nothing.
  • The destination azure subscription should be empty, otherwise it will be overwritten during upgrade
  • Source and destination subscription should have same server administrator assigned until the migration is complete.

 Subscribe to my blog

How to get list of SharePoint Lists in SharePoint 2013 Online using CSOM

In this post, we’ll see how to get the list of the SharePoint List in SharePoint 2013 Online using CSOM.

Open Visual Studio 2013.

File –> New –> Project –> Visual C# –> Console Application  and name it as ‘ReadSharePointLists’

Add a reference to assemblies ‘Microsoft.SharePoint.Client’ and ‘Microsoft.SharePoint.Client.Runtime’.

Import the following two namespaces.

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

Copy and paste the following snippet of the code that will help us to fetch the list of SharePoint lists in the SharePoint Online site.

namespace ReadSharePointLists
{
    class Program
    {
        static void Main(string[] args)
        {
            //Replace it with the url of your tenant or your site-collection
            string SiteUrl = "https://yoursite.sharepoint.com";

            System.Uri oUri = new System.Uri(SiteUrl);

            using (ClientContext oClientContext = new ClientContext(SiteUrl))
            {
                //Replace it with your user id for SharePoint Online
                string UserName = "userid@yoursite.onmicrosoft.com";


                //Replace it with your password
                string Password = "password";

                //Create a SecureString object from password string, needed for SharePointOnlineCredentials class
                SecureString SecurePassword = GetSecureString(Password);
                oClientContext.Credentials = new SharePointOnlineCredentials(UserName, SecurePassword);

                //load the properties of web object
                Web oWeb = oClientContext.Web;
                
                //Get all the lists in the web
                oClientContext.Load(oWeb.Lists);
                oClientContext.ExecuteQuery();


                foreach (List oList in oWeb.Lists)
                {
                    Console.WriteLine(oList.Title.ToString());
                
                }
                Console.ReadLine();


            
            }

        }

        private static SecureString GetSecureString(String Password)
        {
            SecureString oSecurePassword = new SecureString();

            foreach (Char c in Password.ToCharArray())
            {
                oSecurePassword.AppendChar(c);

            }
            return oSecurePassword;
        }

    }
}

 

After executing this we’ll see the below result, which displays the names of the SharePoint lists in the SharePoint 2013 Online site.clip_image002

 Subscribe to my blog

Largest collection of free Microsoft eBooks

Largest collection of FREE Microsoft eBooks ever, including: Windows 8.1, Windows 8, Windows 7, Office 2013, Office 365, Office 2010, SharePoint 2013, Dynamics CRM, PowerShell, Exchange Server, Lync 2013, System Center, Azure, Cloud, SQL Server can be downloaded here in the msdn blog.

http://blogs.msdn.com/b/mssmallbiz/archive/2014/07/07/largest-collection-of-free-microsoft-ebooks-ever-including-windows-8-1-windows-8-windows-7-office-2013-office-365-office-2010-sharepoint-2013-dynamics-crm-powershell-exchange-server-lync-2013-system-center-azure-cloud-sql.aspx

I’d like to thank Eric Ligman for collating this and publishing this in MSDN blog.

 

Configure internet access in Microsoft Azure Virtual Network

I’m setting up a Virtual Network in Azure to host my SharePoint 2013 farm. I’ve configured added the DNS servers of 10.0.0.4 and 10.0.0.5 (as per my pervious article), expecting that I would be able to access the public internet inside the SharePoint 2013 VM’s. I’m able to access only google and few other internet sites inside the VMs, most of the sites including microsoft.com was not accessible.

The fix for this issue is to add the Public DNS servers (168.63.129.16 and 168.62.167.9) to the list of DNS servers in the Azure Virtual Network.

image

After adding the Public DNS servers (168.63.129.16 and 168.62.167.9) , the internet connectivity worked like a charm.

 Subscribe to my blog

Consume a SharePoint 2013 Online REST feed using .NET managed code

In this post, we’ll see how we can consume a SharePoint 2013 online REST feed using managed c# code. I’d be using a Provider Hosted App template in Visual Studio 2013 for this demonstration. It is very straight forward to consume a Restful .NET service by issuing HttpWebRequest from c# code and getting HttpWebResponse.

I will be invoking https://yoursite.sharepoint.com/_api/web/lists from c# code to get the list of SharePoint lists in this Office 365 site. The following screenshot shows the response for this Restful service in the browser.

image

Now let’s start creating a Provider hosted app project in Visual Studio 2013.

File –> New Project –> Visual C# –> Office/SharePoint –> App for SharePoint 2013 and name it as ‘ReadRESTCSharp’ & set the site for debugging and app type as ‘Provider-hosted’.

image

Specify the web project type as ‘ASP.NET Web Forms Application’. In the next step, Visual Studio 2013 would prompt for setting App Authentication Type, I did not see this option while using Visual Studio 2012 to create Provider hosted apps.

image

Set the App authentication type as ‘Use Windows Azure Access Control Service’ (mentioned in the above screenshot).

Click ‘Finish’ to create the app.

Go to  ASP.NET Web Forms project and open the default.aspx.cs

Import the following three namespaces at the top.

using Microsoft.SharePoint.Client;
using System.Net;
using System.IO;

In page_load comment out the following lines of code.

//var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

            //using (var clientContext = spContext.CreateUserClientContextForSPHost())
            //{
              //  clientContext.Load(clientContext.Web, web => web.Title);
               // clientContext.ExecuteQuery();
                //Response.Write(clientContext.Web.Title);
            //}

Since I’m using my own developer tenant in Office 365, I would want to trust all the certificates issued by SharePoint Online to my browser. Otherwise, it will keeping popping up a message to trust the certificates in Visual Studio 2013, when I hit F5 and debug the app.

Open the TokenHelper.cs and the add the following static method ‘TrustAllCertificates’

public static void TrustAllCertificates()
        {
            //Trust all certificates
            System.Net.ServicePointManager.ServerCertificateValidationCallback =
                ((sender, certificate, chain, sslPolicyErrors) => true);
        }

If you are using Visual Studio 2012, it adds ‘TrustAllCertificates’ in TokenHelper.cs by default.

I will be using my Office 365 developer tenant for this demo, which forces the need to get the Access Token from Office 365 and pass it as a part of the header in the HttpWebRequest. First I need to get the ContextTokenString by invoking GetContextTokenFromRequest method in TokenHelper.cs. Then I need to get the actual SharePointContextToken by invoking ReadAndValidateContextToken method in the TokenHelper.cs. Finally I need to get AccessToken by invoking the GetAccessToken method in the TokenHelper.cs. I won’t go too detailed about this one. All the dynamics of the Token exchange is covered in one of my previous article.

Once the AccessToken is received, a HTTPWebRequest needs to be issued by setting the AccessToken with Bearer, also we need to set the HTTPRequest method as ‘GET’ and HTTPRequest accept headers as “application/json;odata=verbose”.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using Microsoft.SharePoint.Client;

namespace ReadRESTCSharpWeb
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            Uri redirectUrl;
            switch (SharePointContextProvider.CheckRedirectionStatus(Context, out redirectUrl))
            {
                case RedirectionStatus.Ok:
                    return;
                case RedirectionStatus.ShouldRedirect:
                    Response.Redirect(redirectUrl.AbsoluteUri, endResponse: true);
                    break;
                case RedirectionStatus.CanNotRedirect:
                    Response.Write("An error occurred while processing your request.");
                    Response.End();
                    break;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
            // The following code gets the client context and Title property by using TokenHelper.
            // To access other properties, the app may need to request permissions on the host web.
            //var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

            //using (var clientContext = spContext.CreateUserClientContextForSPHost())
            //{
              //  clientContext.Load(clientContext.Web, web => web.Title);
               // clientContext.ExecuteQuery();
                //Response.Write(clientContext.Web.Title);


            //}

            TokenHelper.TrustAllCertificates();

            //Get the ContextTokenString
            string ContextTokenString = TokenHelper.GetContextTokenFromRequest(Request);



            if (ContextTokenString != null)
            {

                //Get the SharePointContextToken
                SharePointContextToken ContextToken = TokenHelper.ReadAndValidateContextToken(ContextTokenString, Request.Url.Authority);

                Uri sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]);

                //Get the AccessToken
                string AccessToken = TokenHelper.GetAccessToken(ContextToken,sharepointUrl.Authority).AccessToken;

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"https://yoursite.sharepoint.com/_api/web/lists");
                request.Method = "GET";
                request.Accept = "application/json;odata=verbose";
                request.Headers.Add("Authorization", "Bearer " + AccessToken);

                HttpWebResponse response =(HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());

                Response.Write("<h2>List of SharePoint lists in SharePoint Online fetched from managed code</h2>");
                Response.Write("<p>" + reader.ReadToEnd() + "</p>");
                Response.Flush();
            
            
            }




        }
    }
}

After executing the above we get the successful HTTPResponse which can be seen in debug mode in Visual Studio 2013.

image

  Subscribe to my blog

How to retrieve current user in SharePoint 2013 Online using CSOM

In this post, we’ll see how we can retrieve the current user in a SharePoint 2013 online site using client side object model (CSOM). We’ll  be using a console application for the purpose of this demonstration.

Open Visual Studio 2013

File –> New Project –> C# –> Console application

Add  reference to the following assemblies.

Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.runtime.dll

Import the following namespaces at the top of Program.cs

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

namespace Retrievecurrentuser
{
    class Program
    {
        static void Main(string[] args)
        {

            using (ClientContext oClientContext = new ClientContext(@"https://yoursite.sharepoint.com"))
            {

                //Assign User Id for your SharePoint Online tenant     
                string UserName = "userid@yoursite.onmicrosoft.com";

                //Assign password for your SharePoint online tenant
                string Password = "password";

                //Create a SecureString object from password string, needed for SharePointOnlineCredentials class
                SecureString SecurePassword = GetSecureString(Password);
                oClientContext.Credentials = new SharePointOnlineCredentials(UserName, SecurePassword);


                //load the properties of web object
                Web oWeb = oClientContext.Web;
                oClientContext.Load(oWeb);
                oClientContext.ExecuteQuery();

                //retrieve the site name
                string SiteName = oWeb.Title;

                //retrieve the current user
                oClientContext.Load(oWeb.CurrentUser);
                oClientContext.ExecuteQuery();

            Console.WriteLine("Login Name"+ oClientContext.Web.CurrentUser.LoginName);
                Console.WriteLine("Is Admin"+ oClientContext.Web.CurrentUser.IsSiteAdmin);
                Console.WriteLine("Email"+ oClientContext.Web.CurrentUser.Email);
                
                Console.ReadLine();


            }
        }

        private static SecureString GetSecureString(String Password)
        {
            SecureString oSecurePassword = new SecureString();

            foreach (Char c in Password.ToCharArray())
            {
                oSecurePassword.AppendChar(c);

            }
            return oSecurePassword;
        }

    }
}

Then create a clientcontext in the program.cs, load the web object (execute query) and then load the current user based on the web object.

Now we’ll be able to fetch the user properties like the below.

image

 Subscribe to my blog

How to create a site-collection using CSOM in SharePoint Online

In this post, we’ll see how to create a site-collection using SharePoint 2013 CSOM in SharePoint Online. We’ll be using a console application for the purpose of demonstration.

Open Visual Studio 2013 –> File –> New –> Console Application and name it as ‘CreateSiteCollCSOM’.

Add references to the following assemblies

Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.runtime.dll

Microsoft.Online.SharePoint.Client.Tenant.dll

.

The Microsoft.Online.SharePoint.Client.Tenant.dll will be available in the location C:Program FilesSharePoint Client ComponentsAssemblies, if the SharePoint Client Components SDK is installed.

Import the following namespaces at the top of Program.cs

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

static void Main(string[] args)
        {
            


            using (ClientContext oClientContext = new ClientContext("https://yoursite-admin.sharepoint.com"))
            {
                //Assign User Id for your SharePoint Online tenant     
                string UserName = "userid@yoursite.onmicrosoft.com";

                //Assign password for your SharePoint online tenant
                string Password = "password";

                //Create a SecureString object from password string, needed for SharePointOnlineCredentials class
                SecureString SecurePassword = GetSecureString(Password);

                oClientContext.Credentials = new SharePointOnlineCredentials(UserName, SecurePassword);
                

                var oTenant = new Tenant(oClientContext);

                var oSiteCreationProperties = new SiteCreationProperties();


                //Set the url of site-collection to be created
                oSiteCreationProperties.Url = "https://yoursite.sharepoint.com/sites/TestSiteColl";

                //Set the title of site
                oSiteCreationProperties.Title = "Test SiteColl from code";

                //set the site-collection owner
                oSiteCreationProperties.Owner = "userid@yoursite.onmicrosoft.com";

                //set the template of site-collection to be created as TeamSite
                oSiteCreationProperties.Template = "STS#0";

                //set the storge maxium level in MB
                oSiteCreationProperties.StorageMaximumLevel = 200;

                oSiteCreationProperties.UserCodeMaximumLevel = 100;



                SpoOperation oSpoOperation = oTenant.CreateSite(oSiteCreationProperties);

                oClientContext.Load(oTenant);

                oClientContext.Load(oSpoOperation, i=>i.IsComplete);
                oClientContext.ExecuteQuery();

               

                Console.WriteLine("SiteCollection successfully reated");




            }




        }

        private static SecureString GetSecureString(String Password)
        {
            SecureString oSecurePassword = new SecureString();

            foreach (Char c in Password.ToCharArray())
            {
                oSecurePassword.AppendChar(c);

            }
            return oSecurePassword;
        }

Azure WebJobs – Q&A’s

Here are some important Q&A’s around AzureWebjobs.

1. What is WebJobs in Azure?

The WebJobs in Azure enables us to run programs or scripts in Azure web sites in one of the following 3 ways :-

  • On Demand
  • Continuously
  • Scheduled

 

2. What are the acceptable file types for Scripts to create WebJobs?

It supports the set of file types creating using the following technologies:-

  • using windows cmd (.cmd, .bat and .exe)
  • using powershell (.ps1)
  • using bash (.sh)
  • using php (.php)
  • using python (.py)
  • using node (.js)

3. Where are WebJobs deployed?

WebJobs are deployed as a part of web site or ftp-ed into a specific directory. Once the WebJobs is deployed to the Azure web site, it gives an extra dashboard which details out the job execution history.

4. How to set up a WebJobs?

First you need to create an Azure web site for setting up the WebJobs. Click WebJobs (preview) at the top and click Add at the bottom. Then zip the contents of job folders and project and upload it.

5. What is the significance of Azure scheduler in the context of WebJobs?

To schedule a WebJobs, we need to have Azure scheduler enabled.

6. What is the cost for running WebJobs?

As of now, three is no additional cost for using Azure WebJobs unless AlwaysOn feature is turned on.

7. What is the maximum allowed file size for content (.zip files) submitted to WebJobs?

The maximum allowed file size for the content (.zip files) submitted to WebJobs is 100 MB. The .zip file should contain the executables (in any one of the formats like .exe, .cmd, .bat, .sh, .php, .py and .js).