Anutosh Datta
Anutosh Datta

Reputation: 419

Using salesforce metadata api with C#

How can I create salesforce metadata connection using C#? Thanks in advance.

Upvotes: 3

Views: 2286

Answers (2)

code4j
code4j

Reputation: 4626

A Metadata API example using Enterprise API. The trick is to change the login session header using Enterprise API to MetaAPI client

        string username = "username";
        string password = "password";

        // Create a SoapClient specifically for logging in
        loginClient = new SoapClient();

        LoginResult lr = null;
        try
        {
            Console.WriteLine("\nLogging in...\n");
            lr = loginClient.login(null, username, password);
        }
        catch (Exception e)
        {
            // Write the fault message to the console 
            Console.WriteLine("An unexpected error has occurred: " + e.Message);

            // Write the stack trace to the console 
            Console.WriteLine(e.StackTrace);
        }

        // Check if the password has expired 
        if (lr.passwordExpired)
        {
            Console.WriteLine("An error has occurred. Your password has expired.");
        }

        // end point
        endpoint = new EndpointAddress(lr.metadataServerUrl);
        Console.WriteLine("End point: " + endpoint.Uri);

        // session header
        Metadata.SessionHeader metaSession = new Metadata.SessionHeader();
        metaSession.sessionId = lr.sessionId;

        MetadataPortTypeClient client = new MetadataPortTypeClient("Metadata", endpoint);
        CallOptions callOptions = new CallOptions();
        callOptions.client = lr.userId;

        ListMetadataQuery q = new ListMetadataQuery();
        q.type = "CustomObject";
        FileProperties[] fs = client.listMetadata(metaSession, callOptions, new []{ q} , 39.0);
        foreach (FileProperties f in fs)
        {
            Console.WriteLine("response with message: " + f.fileName);
            Console.WriteLine("response with message: " + f.fullName);
        }

Upvotes: 0

Anutosh Datta
Anutosh Datta

Reputation: 419

Found the answer:

Sforceservice binding = new Sforceservice();
binding.url = <salesforceurl>;
LoginResult lr = binding.login(<username>, <password>);

binding.url = lr.serverurl;
binding.sesionheadervalue = new sessionheader();
binding.sessionheadervalue.sessionid = lr.sessionid;

MetaDataService service = new MetaDataService();
service.sesionheadervalue = new sessionheader();
service.sessionheadervalue.sessionid = binding.sessionid;

Upvotes: 2

Related Questions