Saturday, March 16, 2013

[C#] How to Add Web Reference Dynamically

Issue: Using SharePoint Web Services Lists and Copy in our custom ASP.NET code using C#

Lists: http://www.AnySharePointSite.com/_vti_bin/Lists.asmx
Copy: http://www.AnySharePointSite.com/_vti_bin/Copy.asmx

In Visual Studio (we use 2005 for SharePoint 2007), to use Web service, you can right click on a project and select Add Web Reference. However, we use SharePoint Designer (2007) to add custom ASP.NET code using C# and there is no open to add Web Reference to use in the code.

Solution: Add Web Reference on the fly (dynamically). We created a class WebServices with CallWebService function to add Web reference to invoke any Web service method.

Usage: see upcoming blog...


using System;
using System.Net;                       //NetworkCredential,WebClient
using System.IO;                        //Stream
using System.Security.Permissions;      //SecurityPermissionAttribute
using System.Web.Services.Description;  //ServiceDescription,ServiceDescriptionImporter,ServiceDescriptionImportStyle,ServiceDescriptionImportWarnings -> Reference/.NET/System.Web.Services
using System.CodeDom;                   //CodeNamespace,CodeCompileUnit,importer,provider1
using System.CodeDom.Compiler;          //CodeDomProvider,CompilerParameters,CompilerResults
using System.Reflection;                //MethodInfo

public class WebServices
{
    [SecurityPermissionAttribute(SecurityAction.Demand, Unrestricted = false)]
    //[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
    public object CallWebService(string webServiceAsmxUrl, string serviceName, string methodName, object[] args)
    {
        WebClient client = new System.Net.WebClient();

        //Comment out next 2 lines if use custom credential to access authenticated/secured Web service, replace with username/password/domain
        //NetworkCredential customCredential = new NetworkCredential("USER_NAME", "PASSWORD", "DOMAIN_NAME");
        //client.Credentials = customCredential;

        // Connect To the web service
        Stream stream = client.OpenRead(webServiceAsmxUrl + "?wsdl");

        // Now read the WSDL file describing a service.
        ServiceDescription description = ServiceDescription.Read(stream);

        ///// LOAD THE DOM /////////

        // Initialize a service description importer.
        ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
        importer.ProtocolName = "Soap12"; // Use SOAP 1.2.
        //importer.ProtocolName = "Soap"; // Use this for SharePoint 2003
        importer.AddServiceDescription(description, null, null);

        // Generate a proxy client.
        importer.Style = ServiceDescriptionImportStyle.Client;

        // Generate properties to represent primitive values.
        importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;

        // Initialize a Code-DOM tree into which we will import the service.
        CodeNamespace nmspace = new CodeNamespace();
        CodeCompileUnit unit1 = new CodeCompileUnit();
        unit1.Namespaces.Add(nmspace);

        // Import the service into the Code-DOM tree. This creates proxy code that uses the service.
        ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit1);

        if (warning == 0) // If zero then we are good to go
        {
            // Generate the proxy code
            CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp");

            // Compile the assembly proxy with the appropriate references
            string[] assemblyReferences = new string[5] { "System.dll", "System.Web.Services.dll", "System.Web.dll", "System.Xml.dll", "System.Data.dll" };
            CompilerParameters parms = new CompilerParameters(assemblyReferences);
            CompilerResults results = provider1.CompileAssemblyFromDom(parms, unit1);

            // Check For Errors
            if (results.Errors.Count > 0)
            {
                foreach (CompilerError oops in results.Errors)
                {
                    System.Diagnostics.Debug.WriteLine("========Compiler error============");
                    System.Diagnostics.Debug.WriteLine(oops.ErrorText);
                }
                throw new System.Exception("Compile Error Occured calling webservice. Check Debug ouput window.");
            }

            // Finally, Invoke the web service method
            object wsvcClass = results.CompiledAssembly.CreateInstance(serviceName);
            MethodInfo mi = wsvcClass.GetType().GetMethod(methodName);
            //((System.Web.Services.Protocols.WebClientProtocol)(wsvcClass)).Credentials = customCredential;        //custom credential
            //((System.Web.Services.Protocols.SoapHttpClientProtocol)(wsvcClass)).Credentials = customCredential;   // OR custom credential

            return mi.Invoke(wsvcClass, args);
        }

        else
        {
            return null;
        }
    }
}