Saturday, October 15, 2016

JavaScript Object References

Recently people asked me how JavaScript keep references while passing through function parameters including angular promise, underscore and normal JS code. I will provide one example from each section to show how references are passed.

Under Score (Lodash) Example

Some of the Underscore library functions return object reference while other returns new object. e.g. _.find method returns reference of the object that was found. in the following example when we change the value of variable "f.a", it affects "chkUnder" variable. While in case of _.where method, if we change the value of "w.a", it never changes the variable "chkUnder".

var chkUnder = [{ a: 1 }, { a: 2 }, { a: 3 }];
var f = _.find(chkUnder, { a: 2 });
f.a = 22;
var w = _.where(chkUnder, { a: 2 });
w.a = 24;

Wednesday, July 6, 2016

How to add Web API to an existing ASP.NET MVC Web Application

The steps I needed to perform were:
  1. Add reference to System.Web.Http.WebHost.
  2. Add App_Start\WebApiConfig.cs (see code snippet below).
  3. Import namespace System.Web.Http in Global.asax.cs.
  4. Call WebApiConfig.Register(GlobalConfiguration.Configuration) in MvcApplication.Application_Start() (in file Global.asax.cs), before registering the default Web Application route as that would otherwise take precedence.
  5. Add a controller deriving from System.Web.Http.ApiController.
I could then learn enough from the tutorial (Your First ASP.NET Web API) to define my API controller.
App_Start\WebApiConfig.cs:
using System.Web.Http;

class WebApiConfig
{
    public static void Register(HttpConfiguration configuration)
    {
        configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
            new { id = RouteParameter.Optional });


// To return objects in Json format (Not XML Format),
var appXmlType = configuration.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
            configuration.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

    }
}
Global.asax.cs:
using System.Web.Http;

...

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

Use TypeScript in SharePoint Hosted App

There is trick involved in making Visual Studio realize to compile TypeScript and generate javascript files, Following procedure will give you complete guidance how to complete the process.

1.      Create a new Sharepoint-hosted app.
a.      Right-click the project name and select Unload Project.
b.      Right-click the project name and select Edit <your project name>.csproj.
c.      Add these lines before the last closing <Project> tag. 

    <TypeScriptSourceMap>true</TypeScriptSourceMap>
  </PropertyGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" />

1.      Right-click the project name and select Reload Project.
2.      Now you will see the TypeScript tab in the project properties.


2.      Now you can add the TypeScript definitions. Right-click the project name and select Manage Nuget Packages. Search online for “sharePoint.Typescript.” Install thesharePoint.Typescript.DefinitelyTyped package.      

3.      Also search for “jquery.typescript” and install the jquery.Typescript.DefinitelyTyped package.

Now you are ready to work with Typescript and can create Typescript files.

Tuesday, May 24, 2016

Extract GAC DLL

Most of the times developers need to extract/copy dlls from GAC. The first step is to see whether these dlls are 32 bit or 64 bit or MSIL. So the first step is to open the command prompt and move to assembly folder. To do this, you need to run following command,

cd c:\Windows\assembly

Now you can see how many types of of assembly folders exists (Windows explorer does not let you select these folders). run following command and you will see the results as below,

dir


Monday, May 9, 2016

Operation is not valid due to current state of the object -- Elevated Privileges


Exception

ExceptionType=System.InvalidOperationExceptionJSON
Message=Operation is not valid due to the current state of the object.

Cause: 

I tried to call SPUtility.SendEmail within the RunWithElevatedPrivileges delegate.

Comments

If i try to run this method out of Elevated delegate, it works fine. All the other things that are working fine in Elevated Privileges (except send email) are,

  1. SPUtility.GetPrincipalsInGroup
  2. spWeb.EnsureUser

Thursday, February 11, 2016

How to set InfoPath field value on dropdown change event in SharePoint 2010 and 2013

Problem

We have a Dropdown in InfoPath which is populated from external data source (SharePoint List. e.g. TimeSheet List having fields ProjectName, %Complete and Type. Dropdown is showing Project Names). We want to populate infopath field (Type) from SharePoint based on selected Project in dropdown.

Solution

I suppose you have already Filled Dropdown from Timesheet List and or applied filtering if required, If not, you can follow my post How to Filter Infopath Dropdown.

Select the Project dropdown that we created in previous article and from the "Home" ribbon, select Add Rule->This Field Changes -> Set a Field's Value option as shown in the below image.

How to Populate and Filter InfoPath Dropdown Populating from SharePoint List

Problem

We have TimeSheet SharePoint List which has information of all the projects, their status and % Complete. We want to populate dropdown with TimeSheet projects whose status is not closed.

Solution

Populating Dropdown

First Create the List as following,


Now in the InfoPath Designer (It does not matter if it is list template or designing new InfoPath document), Add dropdown list from menu bar,

Tuesday, February 2, 2016

Unable to connect to the Synchronization Service error when you try to open Miisclient.exe


Problem

When you try to open Miisclient.exe , you get the following error message:
Unable to connect to the Synchronization Service.

Some possible reasons are:

1) The service is not started

2) Your account is not a member of a required security group.

See the Synchronization Service documentation details.