Monday, February 23, 2015

How to Send Email in SharePoint Provider Hosted Apps

There are few methods that we can use to send Emails in SharePoint Provider Hosted apps.

  • Using general Email Sending method
  • Using SharePoint Client Object Model (CSOM)
  • Using SharePoint JavaScript Model (JSOM)

Using general Email Sending method

This is the general method we are using for sending email for This is method has advantages over other two methods. this method has few advantages over other methods,

  • Send attachments to the recipients
  • Send emails to external users (SharePoint 2013 email function can not be used to send emails to external users) 

There are many articles in available for this , I’m describing a sample code below.

MailMessage mail = new MailMessage("", "");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";

For this you need to either have a smtp server setup by your self or valid authentication for the existing server.

Using SharePoint Client Object Model (CSOM)

This has been a most famous method for a sending email. You can use SharePoint Utility class to send a Email. But you can not send to external users. If you are sending to external users they should be added to your mail exchange but it will take some time to reflect the change.

var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
    var emailp = new EmailProperties();
    emailp.BCC = new List<string>{""};
    emailp.To = new List<string>{""};
    emailp= "";
    emailp.Body = "<b>html</b>";
    emailp.Subject = "subject";
    Utility.SendEmail(_clientContext, properties);

You can use the same method with remote event receivers only by changing SharePoint client context initiation. 

Using SharePoint JavaScript Model (JSOM)

This is very similar to the CSOM but it will use only JavaScript for sending emails.

var mail = {
        properties: {
            __metadata: { 'type': 'SP.Utilities.EmailProperties' },
            From: '',
            To: { 'results': ['',''] },
            Body: 'some body',
            Subject: 'subject'
var getAppWebUrlUrl = decodeURIComponent(utils.getQueryStringParameter("SPAppWebUrl").replace("#", ""));
var urlTemplate = getAppWebUrlUrl + "/_api/SP.Utilities.Utility.SendEmail";
            contentType: 'application/json',
            url: urlTemplate,
            type: "POST",
            data: JSON.stringify(mail),
            headers: {
                "Accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose",
                "X-RequestDigest": $("#__REQUESTDIGEST").val()
            success: function (data) {
                // code
            error: function (err) {
                // code

Sunday, February 8, 2015



How I got this error

it is strange. My app was working perfectly until I change some list definition in my source. suddenly I got this error. I tried to revert the definition and deploy it again but still it gave the error.

You can try following steps


Retract the app using visual studio, and check the site collection whether it is available in Site Contents. if so first remove it.


Then check the app availability in Apps Packages and Apps on testing lists. if it is there delete it.


Then check the recycle bin and Admin recycle bin and empty if any.



and tried to deployed the app.

If it is not working you may have App deployed to other sub sites as well. So go there and delete every thing.

If it is a O365 , go to Share Point administration



then Go to Apps –> App permissions and  delete the items related to the app.


if this wont work , try with new site collection.

Thursday, January 22, 2015

Understanding the Global Assembly Cache (GAC) and Managing , Copying Dlls from Global Assembly Cache

Most of you may confuse why there are two GACs available in the machine.

  • C:\WINDOWS\assembly
  • C:\WINDOWS\Microsoft.NET\assembly

What is GAC

GAC is a shared location in your machine that keeps assemblies, Dlls for common sharing purpose. Assume that you are using in your project. so one option is you can place the dll with your project. but assume you are creating another project which is also using the same .net assembly so it is better to have it in a common place which that can be referred.

since it is a common framework dll and used separately from your application logic it is good to place in a shared place for easy of maintenance and update process.

What is  C:\WINDOWS\assemblyimage

Previous CLRs uses this place as its common location for Dlls. previous CRLs includes versions 1.0,1.1,2.0. these CRLs versions are used by before .NET framework 4.

.NET Framework 1.0  - CLR v1.0
.NET Framework 1.1  - CLR v1.1
.NET Framework 2.0  - CLR v2.0
.NET Framework 3.0  - CLR v2.0
.NET Framework 3.5  - CLR v2.0

so if you are working with application belongs to these versions; this is the place for your global assembly cache.

What is  C:\WINDOWS\Microsoft.NET\assembly

After CLR version 4.0 Microsoft change their location to this for maintaining and avoid loading wrong Dlls in applications for backward compatibility.

.NET Framework 4.0  - CLR v4.0
.NET Framework 4.5  - CLR v4.0
.NET Framework 4.5.1  CLR v4.0
.NET Framework 4.5.2  CLR v4.0

so if you are engaging with application after .NET Framework 4.0; this is the default location for it’s assemblies.

Managing and Copying Dlls from Global Assembly Cache

  • shfusion.dll
  • Gacutil.exe


this is a windows shell extension which shifts before framework 4.0 to manage Dlls in GAC.

if you navigate to  C:\WINDOWS\assembly from your file explorer you can see something like this that you can not manipulate.


so you can use following command to make it manipulate.

  • Goto command prompt (cmd) and open it as run as Administrator
  • then run the command
C:\Windows\system32>regsvr32 /u  C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\shfusion.dll



When you click enter it will prompt with a message like below which turn your GAC into manageable state.

if you navigate to C:\WINDOWS\assembly ; you can see it by your self.


You can revert it back to normal by registering the dll again

C:\Windows\system32>regsvr32 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\shfusion.dll

shfusion.dll is only available till framework 4.0 and its versions. Now it is discontinued and not shipping with the framework. if you are having multiple versions installed use the latest framework folder.

EX:- Microsoft.NET\Framework\<.NET version> \shfusion.dll


This the alternative if you are dealing with Dlls after  4.0 you need to use the gacutil.exe. you can type gacutil in visual studio command prompt and get list of command available for dll manipulating.


Friday, January 16, 2015

How to open Documents in Office Web Apps in SharePoint apps (Provider Hosted and SharePoint Hosted)

Office web apps are now getting more and more popular with the SharePoint 2013 environment. In native SharePoint environment when you click on a document it will open in a office online (OWA) based on configuration.

But it you a writing a SharePoint Hosted app or a Provider Hosted app; you might wonder how to open it in office web apps and what is the URL you are going to use it.

You can use JSOM and SharePoint REST services to archive this.

Frist you need to send a REST request by mentioning file name.

url: AppWebUrlUrl + "/_api/web/GetFileByServerRelativeUrl('server relative file name')",
method: "GET",
contentType: "application/json;odata=verbose",
headers: { "Accept": "application/json; odata=verbose" }

You should carefully give the sever relative file name for this operation. regardless of app type (Provider hosted or a SharePoint Hosted ) file name it coming with app web url


Absolute File Url

Relative File Url


Depending on the App type either you need to use the RequestExecutor or normal rest approach.

Using RequestExecutor in Provider Hosted Apps

var executor = new SP.RequestExecutor("app web url"); // you need to pass app web url
url: "app web url" + "/_api/web/GetFileByServerRelativeUrl('relative doc url')",
method: "GET",
contentType: "application/json;odata=verbose",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
    var arry = JSON.parse(data.body).d;
    if (arry.LinkingUrl) {
        // can open with office web apps
        var owaurl = "app web url" + "/_layouts/15/WopiFrame.aspx?sourcedoc={" + arry.UniqueId + "}&file=" + encodeURIComponent(arry.Title) + "&action=default";                   
    } else {
        // can not open with office web apps (ex:txt)
error: function (data, errorCode, errorMessage) {
// error

if the LinkingUrl available it indicates it can be open via office web apps. LinkingUrl some times returns direct web app url ; thus you don’t need to create the Url by amending items but some times it returns absolute document Url. mentioned approach will be suits for both occasions.  

Ajax Request in SharePoint Hosted Apps

$.Ajax({ // Jquery needed
url: "app web url" + "/_api/web/GetFileByServerRelativeUrl('relative doc url')",
method: "GET",
contentType: "application/json;odata=verbose",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
    var arry = JSON.parse(data.body).d;
    if (arry.LinkingUrl) {
        // can open with office web apps
        var owaurl = "app web url" + "/_layouts/15/WopiFrame.aspx?sourcedoc={" + arry.UniqueId + "}&file=" + encodeURIComponent(arry.Title) + "&action=default";                   
    } else {
        // can not open with office web apps (ex:txt)
error: function (data, errorCode, errorMessage) {
// error