Friday, 17 April 2015

Setting the content type of a document uploaded to SharePoint Library through CSOM

There might be times when we will have to specify a content type for a file that is programmatically uploaded to a SharePoint library through CSOM. This blog post explains the steps involved in setting the custom content type through CSOM code. In order to set the content type we will be using the ParseAndSetFieldValue method of the list item and set the id of our desired content type to the ContentTypeId property.

The first step is to get the content type if of our content type. This is done by looping through all the content types attached to the library and getting the id of the particular content type.

string serverUrl = "SharePoint Site URL";

     String login = "user name";

            String password = "password";

            string listUrlName = "Library Name";

            string customcontenttypename = "Content Type Name";
 

            ClientContext context = new ClientContext(serverUrl);

           
               var passWord = new SecureString();

                foreach (char c in password.ToCharArray()) passWord.AppendChar(c);

                context.Credentials = new SharePointOnlineCredentials(login, passWord);

           List splist = context.Web.GetList(listUrlName);

             context.Load(splist);

             context.Load(splist.ContentTypes);

             context.ExecuteQuery();

              ContentType ctcustom = null;

foreach (ContentType ct in splist.ContentTypes)

{

if (ct.Name == customcontenttypename)

{
ctcustom = ct;

                               break;
}

}

Then we upload the file to the library

var folder = context.Web.GetFolderByServerRelativeUrl(listUrlName);

context.Load(folder);

var files = folder.Files;

context.Load(files);

       string filepath = @"File Path";

                  
       FileInfo fi = new FileInfo(filepath);

       var fci = new FileCreationInformation

{

Url = fi.Name,

Overwrite = true,

Content = System.IO.File.ReadAllBytes(filepath)
};

 
var file = files.Add(fci);
 
And then we set the content type of our custom content type to ContentTypeId property of the list item attached to the uploaded file

file.ListItemAllFields.ParseAndSetFieldValue("ContentTypeId", ctcustom.Id.ToString());

file.ListItemAllFields.Update();

context.Load(file);  

context.ExecuteQuery();

Monday, 23 March 2015

Creating your first Power BI Visualization Dashboard


Microsoft Power BI is a cloud based BI service allows us to create and share reports and data visualization from variety of data sources on any device. The service which was previously available only in the US region is now available worldwide. In this blog we will see how to create a simple visualization in Power BI using excel sheet as Data Source.

Below are the series of steps that have to be taken to create a Power BI visualization

1.       Create a excel data source. For example create a products sheet with Product, Quarter, Revenue, Units Sold columns and enter data for few rows. Select the rows and create a table.

 

2.       Sign in to Power BI , if you don’t have an account sign up for free using your official email.

3.       In the Power BI designer click ‘+’ sign next to the Datasets menu in left navigation pane.

 
4.       You will be taken to Get Data page. On this page click Excel Workbook in the left navigation and then click Connect button

 

5.       Choose the location as computer and Browse to the location where we have saved the excel workbook with table and click Connect button


6.       A new dashboard will be created with the selected excel work book.

7.       Click on the dashboard from the left navigation pane and then click source to create a new visualization.


 

8.       On the visualization page select the fields you want in the visualization for example Product and Units sold fields. The Visualization will be displayed on the screen.

 
9.       Save the report by clicking the Save menu from top and give a name to the report.

10.       Select the visualization to by clicking on it and then click the Pin button on the right to pin the visualization to the dashboard.

11.   Click on the dashboard name from the top to go back to the dashboard. You will see that the visualization has been added to the dashboard.
 

 

12.   Q&A feature in Power BI allows us to explore data through natural language queries and get answers in the form charts. We can create a variety of  Q&A like for example “show products sold more than 2500 in q2”


12.   Visualizations created from Q&A can be pinned to the dashboard.

13.   Dashboards can be easily shared with your colleagues by simply clicking the Share icon and specifying the email addresses of colleagues with whom the report has to be shared.

 
 
Power BI is easy and intuitive to use BI tool that allows us to create, share and access reports and visualizations just using a browser.