Tuesday, April 29, 2014

Could not load file or assembly '$SharePoint.Project.AssemblyFullName$' or one of its dependencies - WCF services in SharePoint

Requirement: Create a WCF service inside SharePoint Layouts folder so that it can be used with in SharePoint objects such as WebParts comfortably.

Note: WCF Services development for SharePoint is not covered here.

Issue: After deploying the service to Layouts using Visual Studio IDE, it throws an error while browsing the service.

Error details:
"System.IO.FileNotFoundException: Could not load file or assembly '$SharePoint.Project.AssemblyFullName$' or one of its dependencies. The system cannot find the file specified.    at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)     at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)     at..........
 "

Solution:
By default in SharePoint 2010, the .SVC templates package is not configured, We need to configure this with a change the target file.

Steps are provided below:
1. Check for the target file (Microsoft.VisualStudio.SharePoint.targets) in the following localtion:
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\SharePointTools
2. Open the file in a Notepad or Textpad and search for  ;ascx and and in the same line add ;svc as given below:
$(TokenReplacementFileExtensions);xml;aspx;ascx;webpart;dwp;svc
3. Save the changes and close it.
Deploy the project that has WCF service (SVC) files from Visual Studio. This should resolve the issue.
Note: This change is required in each developer machines that deploy the solution using Visual Studio. This change is not required for Non Development Environments like Test or Production (where visual studio deployments are not possible) since the WSP that is generated from a Developer machine contains the required SVC file in correct format



Enable PowerShell ISE in Windows 2008 R2 server

Requirement: Enable PowerShell ISE in windows 2008 R2 server for script

Solution Steps:
1. Open SharePoint POwerShell and run the below commands
  a)    Import-Module ServerManager
 b) Add-WindowsFeature PowerShell-ISE 


Saturday, April 12, 2014

Document ID Service Feature not found

Though we were using SharePoint 2010 enterprise version, I could not find the Document ID Service Feature in one of our Web Application’s site collection. There may be a script that can help to enable it, but I found codeplex’s SharePoint Manager 2010 helpful, so depicted the steps below:

1. Open SharePoint 2010 Manager tool, expand _SharePoint_Config node and expand Feature Definitions as depicted below:

image

2. Look for Document ID service feature (DocId), right lick on it and select install as depicted below:

image

After install, this should show the Document ID Service feature under the site collection features

Word Automation Services Error (WAS)

 

In SharePoint 2010, the Word Automation Services by default work in sandbox mode. if you see errors WAS error ULs, the first thing we need to check is if it is running in Sandbox mode, if so disable it using the below script. Usually IISReset after the change may work, but the restart of the SharePoint Hosted Services (Usually app server) server will help.

clear
Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.Powershell

$sp = Get-SPServiceApplication | where {$_.TypeName.Equals("Word Automation Services")} 
$sp.DisableSandbox = $true
$sp.Update()

Sunday, April 21, 2013

Contract First Approach for ASP.Net Web Services



Requirement: Client uses IBM ESB and expects the WSDL and XSD files that follow ESB standards

Issue: The auto generated WSDL and XSD from ASMX (http://url.asmx?WSDL) don’t support the ESB standards because of .Net limitation in providing XMLElement Attributes

Solution: Build XSD and WSDL manually and generate Service implementation and Proxy classes using WSDL.EXE tool provided by .Net framework. The possible Implementation classes can be Abstract class and Interface. Once implementation file is generated implement the real business logic by extending the implementation class or interface in to service file (.asmx).

Steps to create:
1.      The quick way to build XSD and WSDL is to create an ASP.Net Webservice and add the web methods we are planning to implement.
2.      Run the webservice and go to WSDL using http://localhost/file.asmx?WSDL
3.      Capture the WSDL and make a copy of its content
4.      You will find XSD definition under tag.
a.        If you want to separate XSD from WSDL you can copy that and make an XMLSchema file (.xsd) and start building it in visual studio

Referencing XSD into WSDL like below:


<wsdl:types>
    <xsd:schema targetNamespace="http://sampledomain/Service" elementFormDefault="qualified">
      <xsd:import namespace="http://sampledomain/service" schemaLocation="Service.xsd" />
    </xsd:schema>
  </wsdl:types>


b.      If you don’t want to separate XSD from WSDL you can edit in WSDL itself (of course using Visual Studio)
Note: There are some tools out which can help you to build WSDL and XSD but I felt comfortable using Visual Studio.
5.      Define all the elements required in XSD and define messages and other elements in WSDL
6.      Now, it is the time to generate Service files from WSDL using WSDL.EXE. I was using .Net 3.5 and my system has only VS IDE 2010 installed.  VS 2008 Framework was there but no IDE installed. So used the below command and register VS 2008 assemblies using RUN.
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\regasm.exe

7.      Run WSDL.EXE and generate the class files like below:
a.)    Generate Service Interface using the command given below:

C:\Windows\Microsoft.NET\Framework\v2.0.50727>wsdl  /si  /out:IUserManagementServi
ce.cs " D:\Users\POCs\WebApplication1\WebApplication1\SampleService.ws
dl"  "D:\Users\POCs\WebApplication1\WebApplication1\SampleService.xsd"

b.)    Generate Service Abstract class using the command given below:
C:\Windows\Microsoft.NET\Framework\v2.0.50727>wsdl /server /out:SampleServi
ceBase.cs "D:\Users\POCs\WebApplication1\WebApplication1\SampleService.ws
dl"  "D:\Users\POCs\WebApplication1\WebApplication1\SampleService.xsd"

/si: Generates the Interface
/server Generates the Abstract class

Note: You should use only one file either it is abstract class or interface in your Webservice project.

c.)    Generate the Service Proxy for Client for testing.
C:\Windows\Microsoft.NET\Framework\v2.0.50727>wsdl  /out:SampleServi
ceProxy.cs "D:\Users\POCs\WebApplication1\WebApplication1\SampleService.ws
dl"  "D:\Users\POCs\WebApplication1\WebApplication1\SampleService.xsd"

These files need to be regenerated every time you make a change in XSD and WSDL files.

8.      Let’s take Interface implementation over abstract class (I did both, but liked Interface). Go to Web Service Project in Visual Studio and add the Interface file, WSDL and XSD files. The interface will have all the signatures of WebMethods. Now go to your service file (.asmx.cs) and implement the interface (you can remove the previously added webmethods, including helloworld webmethod). It will look like below:
public class SampleService : System.Web.Services.WebService, ISampleServiceSOAP

To implement interface methods in Service class, right click on interface (ISampleServiceSOAP) right click on it and select “Implement Interface” and you will see two options and between them select “Implement Interface”. This will add all the interface methods. We can add our business logic in these methods.

Now the service is ready with contract approach!!

9.      Time for testing. Create a client application (Console or Web App or Window) and take the proxy class file that was created at step 7c and add it to Client Project.
10.   Create an object on Proxy Service class and call a web method with proper input values and test it.

Note:
1.      It is always good idea to host your service in your local IIS server with proper port number for testing it locally. This way you can ensure that the service can be accessed from client using URL
2.      Also make sure that the Proxy Service class constructor has the url  rightly pointing to the service url (.asmx) which provides service end points
Ex:
public SampleService()
{
        this.Url = "http://localhost:9999/SampleService.asmx";
}



Online references:

Access to XMLHttpRequest at 'from origin has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. .net core angular

Issue: The angular application was getting error from API that the origin has been blocked by CORS policy. Solution: Make sure that the...