Wednesday, 2 June 2010

Calling a Web Service in Sandbox Solution

In order to call a web service in sandbox environment following steps should be followed.

Note: Assuming you already have created the web service and deployed it in IIS

1. Create a class library project and which is derived from “SPProxyOperation” (Microsoft.SharePoint.UserCode namespace).


Code Snippet

class ProxyProvider : SPProxyOperation
{
public override object Execute(SPProxyOperationArgs args)

{

if
(args != null)
{

ProviderSoapClient client = new ProviderSoapClient();

ProviderArgs providerArgs = args as ProviderArgs;

return client.GetDepartmentByCode(providerArgs.DeptCode);

}

else

{

return "Error while calling Web Service...";

}

}

}

2. Create another class which should be used for passing arguments to the SPProxyOperation class



Code Snippet

[Serializable]

public class ProviderArgs : SPProxyOperationArgs
{
public int DeptCode
{
get;
set;
}
public ProviderArgs(int code)
{
this.DeptCode = code;
}
}

3. Argument class must have “Serializable” attribute.

4. Implement Execute method of SPProxyOperation class.

5. Attach below attribute with the assembly

[assembly:System.Security.AllowPartiallyTrustedCallersAttribute()]

6. Make sure assembly is strongly named.

7. Deploy the assembly in GAC using GACUTIL(Since drag and drop assembly gives access denied error).

8. Register the assembly in Sharepoint

To register a assembly in Sharepoint “Power Shell” can be used.

Write below scripts in a .ps1 file and run from power shell

Param($assemblyName, $typeName)

$userCodeService = [Microsoft.SharePoint.Administration.SPUserCodeService]::Local

$proxyOperationType = new-object -typename Microsoft.SharePoint.UserCode.SPProxyOperationType -argumentlist $assemblyName, $typeName

$userCodeService.ProxyOperationTypes.Add($proxyOperationType)

$userCodeService.Update()

9. Since this is full trust proxy class so it would run under “SPUCWorkerProcessProxy.exe” so add the web service related configuration setting in SPUCWorkerProcessProxy.exe.config file which can be found under “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\UserCode” folder.

10. Now you are ready to call the web service method via Proxy operation class. Use below method to call the web service from your Sandbox Solution.


SPUtility.ExecuteRegisteredProxyOperation("AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fffdd72d4537ce50", "NameSpace.ClassName", new ArgumentClass(Argument));

2 comments:

Brian said...

I just posed a fully functional demo on my blog. Check it out. http://blog.brianbeach.com/2011/09/sharepoint-2010-full-trust-proxy.html

Doc WattsMan said...

Thanks for leading me in the right direction. Too bad that means we won't be able to call any web services in Office 365. Maybe the new capabilities for BCS connections will provide that, but who can say.

Query List/Document Library in Specific Folder

To query SharePoint List or Document Library in specific Folder “ FolderServerRelativeUrl ” as part of the CAML Query Code Snippet ...