Thursday, September 29, 2016

Export webparts in a sharepoint page

A sharepoint page has markup and webparts zones inside the markup. When a user adds webparts to the web part zones, the markup for added webparts is not stored directly inside the page, but rather inside database tables. Hence the webpart markup can not be seen if users download a copy of the page, neither does sharepoint designer show webpart markup. 

There are few situations when we need the webparts markup, e.g. copying page to some other farm, backup on local folder, etc. The blog at https://chuvash.eu/2014/09/19/export-any-web-part-from-a-sharepoint-page/ mentions several different ways to export webparts in a page, in particular I liked the idea of running javascript at https://github.com/mirontoli/sp-lend-id/blob/master/pajla/sp-export-webpart.js in console window.

However since my old days with SharePoint 2007 I know another method to export all webparts in a sharepoint page along with markup of the webpart page itself. Here it goes:


1) Open the document library (Pages library in most cases) in SharePoint designer
2) Copy the webpart page say page1.aspx and paste it, this creates a new webpart page with name like page1_copy(1).aspx
3) Right click on page1_copy(1).aspx and select "Detach from Page Layout" option
 4) Click "Yes" to the dialog prompt
5) Right click on page1_copy(1).aspx and choose "Open" or "Edit file in Advanced Mode"
6) Copy contents and paste in a local file. 

Wednesday, September 28, 2016

Get public key token of an assembly using visual studio

This blog explains a very convenient way to get public key token of a visual studio project assembly.
I am repeating here since path has changed a little.

In short: Add an external tool in visual studio with the following properties

  1. Title: Get &PublicKeyToken
  2. Command: C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6 Tools\sn.exe
  3. Arguments: -Tp $(TargetPath)

Reference:

Modify SharePoint application page using IHTTPModule

There are various ways to modify a sharepoint oob application page, with pros and cons, but the method described in blog post below is one of the cleanest ways I have come across.

https://blogs.msdn.microsoft.com/sasohail/2010/12/18/modifying-sharepoint-application-pages-in-the-right-way/


Monday, September 26, 2016

Post complex objects as parameters to web api controllers

I have an API project and want to call a web method asynchroneously from client project.

The easiest way to do this is to use code like

HttpClient.PostAsJsonAsync<T>(T value) sends application/json
HttpClient.PostAsXmlAsync<T>(T value) sends application/xml

Widget widget = new Widget()
widget.Name = "test"
widget.Price = 1;

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:44268");
client.PostAsJsonAsync("api/test", widget)
    .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode() );

Copied from http://stackoverflow.com/questions/10304863/how-to-use-system-net-httpclient-to-post-a-complex-type

To call a method with simple parameters use

string url = "http://myserver/method";    
string content = "param1=1&param2=2";
HttpClientHandler handler = new HttpClientHandler();
HttpClient httpClient = new HttpClient(handler);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
HttpResponseMessage response = await httpClient.SendAsync(request,content);

Copied from http://stackoverflow.com/questions/27376133/c-httpclient-with-post-parameters

To debug the posted JSON, use JObject parameter type at the web api method
/* Production */
public HttpResponseMessage TrackingRecord([FromBody] TrackingRecord record)
{
    ....
}

/* Debugging */
public HttpResponseMessage TrackingRecord([FromBody] JObject json)
{
    ....
}
Copied from http://forums.asp.net/t/2042177.aspx?Posting+Json+from+C+app+to+Web+API+fails

Thursday, September 1, 2016

WCF web service content type error

After adding a new wcf service to my MVC application, when I tried to post a request, the application responded with error:

POST http://localhost:63342/ISAPI/Service1.svc/MyMethod 415 (Cannot process the message because the content type 'application/json' was not the expected type 'text/xml; charset=utf-8'.)

It can be resolved by adding Factory="System.ServiceModel.Activation.WebServiceHostFactory" to the .svc file markup like so:

<%@ ServiceHost Language="C#" Debug="true" Service="MyWebsite.ISAPI.Service1" CodeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

Get current user id in MVC

Getting current users id has been made very simple with Microsoft.AspNet.Identity.Core nuget package,

First off install the nuget package
Install-Package Microsoft.AspNet.Identity.Core


Now add this using directive in class
using Microsoft.AspNet.Identity;

And finally fetch the current users id by
HttpContext.Current.User.Identity.GetUserId();

Secure micro services using jwt and ocelot

  Secure Microservices Using JWT With Ocelot in .NET Core (code-maze.com)