Get records using Salesforce Partner WSDL in .net

G

Hi,

If you want to fetch the records from Salesforce object’s by using Partner WSDL in .net
then use this code. It also fetch the number of records available in Salesforce custom
and standard object.

Step 1 : Create new Web-service project in .net

Step 2 : Add your Partner WSDL  in your new project. For detail instruction Click here

Steps 3 :  Add new Item to your project and name it as “test.aspx” and add following code to it:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using sforce;

public partial class login : System.Web.UI.Page
{
NanoDictionary rbm = null;
SforceService bind = null;
String tableName, whrval, whrval1, likeQuery, simpleQuery;
List<String> lst = null;
protected void Page_Load(object sender, EventArgs e)
{
//Initialisations
String uname = “user@unsername.com”;
String pass = “Password+SecurityToken”;
String ur = “http://login.saleforce.com”;
rbm = new NanoDictionary(uname, pass, ur);
//Login to Salesforce
bool bl = rbm.loginSample();
Response.Write(“Login Success: ” + bl + “n”);
Response.Write(“<br>”);
lst = new List<string>();
lst.Add(“Name”);
lst.Add(“Gender__c”);
lst.Add(“Email”);
tableName = “Lead”;
whrval = “al”;
whrval1 = “Male”;
likeQuery = lst.ElementAt(2) + ” Like ‘%” + whrval + “%'”;
simpleQuery = lst.ElementAt(1) + ” = ‘” + whrval1 + “‘”;
//Call Method to get record count
int rec = rbm.getRecordsCount(tableName, simpleQuery+” or “+likeQuery);
if (rec > 0)
{
Response.Write(“Number Of Records: ” + rec+”<br>”);
}
else
{
Response.Write(“No Record found<br>”);
}
//Fetch Records
String name, gender, eml;
if (bl == true)
{
bind = rbm.binding;
QueryResult qres = rbm.getRecords(tableName, lst, simpleQuery+” or “+likeQuery, 2, 10, false);
if (qres != null)
{
//Convert & show Result
Response.Write(“<br>Table Name: <b>” + tableName);
Response.Write(“</b><br><br>”);
Response.Write(“<table border = ‘0’ width=’50%’>”);
Response.Write(“<tr>”);
Response.Write(“<th align=’left’> Name </th>”);
Response.Write(“<th align=’left’> Gender </th>”);
Response.Write(“<th align=’left’> Email </th>”);
Response.Write(“</tr>”);
for (int j = 0; j < qres.records.Length; j++)
{
sforce.sObject obj = qres.records[j];
name = obj.Any[0].InnerText;
gender = obj.Any[1].InnerText;
eml = obj.Any[2].InnerText;
Response.Write(“<tr><td width=’15%’>”);
Response.Write(name);
Response.Write(“</td><td width=’10%’>”);
Response.Write(gender);
Response.Write(“</td><td width=’15%’>”);
Response.Write(eml);
Response.Write(“</td></tr>”);
}
Response.Write(“<table><br><br>”);
}
else
{
Response.Write(“<font color=’red’>Query parameter: ” + rbm.inputQry);
Response.Write(“</font><br>Please check Syntax for query parameter”);
}
}
}
}

Step 4: Create a c# class in this project and name it “NanoDictionary” and paste following code in it.

using System;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using sforce;
using System.Collections.Generic;

public class NanoDictionary
{
//Other Variables
public String username, password, url, inputQry;
public SforceService binding;
QueryResult qres;

//Constructor
public NanoDictionary(String username, String password, String url)
{
this.username = username;
this.password = password;
this.url = url;
}
//Method to count records
public int getRecordsCount(String tableName, string query)
{
int size = 0;
String qry = "select Id from " + tableName;
try
{
if (query != string.Empty)
qry += " where " + query;
qres = binding.query(qry);
size = qres.size;
}
catch (Exception ex)
{
//In case of any error -1 would be returned.
Console.Write("ex: " + ex);
size = -1;
}
//For succesful execution, return no. of records.
return size;
}
//Method to check if requested fields are available in specified object
public bool checkFields( String tblname, List fldlst)
{
Field[] flds = null;
bool chk = true;
List tempList = new List();
try
{
DescribeGlobalResult dgr = binding.describeGlobal();
DescribeGlobalSObjectResult[] dgsor = dgr.sobjects;
DescribeSObjectResult dsor = binding.describeSObject(tblname);
if (dsor != null)
{
flds = dsor.fields;
for (int i = 0; i < flds.Length; i++)
{
tempList.Add(flds[i].name);
}
}
else
{
}
IEnumerable check = fldlst.Except(tempList);
if (check.Count() != 0)
{
chk = false;
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Error: " + e.Message);
}
return chk;
}
//Method to login in salesforce
public bool loginSample()
{
Boolean success = false;
// Create a service object
binding = new SforceService();
LoginResult lr;
try
{
lr = binding.login(username, password);
// Save old authentication end point URL
String authEndPoint = url;
// Set returned service endpoint URL
binding.Url = lr.serverUrl;
binding.SessionHeaderValue = new SessionHeader();
binding.SessionHeaderValue.sessionId = lr.sessionId;
success = true;
if(success == true)
System.Diagnostics.Debug.WriteLine("Login");
GetUserInfoResult userInfo = lr.userInfo;
System.Diagnostics.Debug.WriteLine("User Name: " + username);
System.Diagnostics.Debug.WriteLine("UserID: " + userInfo.userId);
System.Diagnostics.Debug.WriteLine("User Full Name: " + userInfo.userFullName);
System.Diagnostics.Debug.WriteLine("User Email: " + userInfo.userEmail);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("An unexpected error has occurred: " +e.Message + "n" + e.StackTrace);
}
return success;
}
public QueryResult getRecords(String tableName, List fldName, String query, int skip, int take, bool more)
{
binding.QueryOptionsValue = new sforce.QueryOptions();
binding.QueryOptionsValue.batchSize = 250;
binding.QueryOptionsValue.batchSizeSpecified = true;
String qry = "select ";

//Check the fields are available in specified object
bool chk = checkFields(tableName, fldName);
if (chk == false)
{
return null;
}
else
{
List fldlist = new List();
fldlist.AddRange(fldName);
for (int i = 0; i < fldlist.Count(); i++)
{
qry += fldlist[i] + ", ";
}
qry = qry.Substring(0, qry.Length - 2);
qry += " ";
qry += "from " + tableName;

if(query != String.Empty)
{
qry += " where " + query ;
}
qry += " limit " + take;
inputQry = qry;
System.Diagnostics.Debug.WriteLine("qry: " + qry);
bool tt = true;
try
{
qres = binding.query(qry);
}
catch (Exception er)
{
//If Query is not in correct format, return false.
Console.Write("er: " + er);
tt = false;
}
if (tt == true)
{
Console.Write("tt: " + tt);
return qres;
}
else
{
return null;
}
}
}
}

Thanx.

About the author

prashant.wayal
By prashant.wayal

Category