Friday, August 31, 2007

Get BDC Data In Sharepoint with Web Service

Get the Users from Business Data Catalog (BDC) programmatically

I got a chance to work in getting users from BDC in sharepoint.

Initially I tryed by getting users from UserInformationList using CAML query
after that I got web service things which made my job easy


The BDC allows you to create a XML metadata file describing a database or web service to SharePoint so that SharePoint can query this data source to make its data available within the portal. Once registered in your SharePoint environment this data can be displayed in the Business Data web parts (described later in this posting) or as lookup information for list/library columns (and column templates).


user this Web service Interface to get user profiles

http://localhost/_vti_bin/userprofileservice.asmx


The below code will give me User data from particular user


public string getUserValuesfromBDC(string attribute)

{ string myvalues = string.Empty;

try
{




string userName = getUserName(); // "VMTPG\\b" ;

userprofile.UserProfileService myuser = new UserProfileService();
userprofile.PropertyData[] userprop = null;

myuser.Credentials = System.Net.CredentialCache.DefaultCredentials;
long test = myuser.GetUserProfileCount();
userprop = myuser.GetUserProfileByName(userName);
for (int i = 0; i <>

{
if (userprop[i].Name != null)

{
if (userprop[i].Name.Trim().ToLower() == attribute.Trim().ToLower())

{
try
{
if (userprop[i].Values[0] != null)
{
myvalues = userprop[i].Values[0].Value.ToString();
return myvalues;

}
}
catch { }

}
}
}


}
catch

{ }

return myvalues;
}





To Get All Users as Data Set


protected void Button1_Click(object sender, EventArgs e)
{

GridView1.AutoGenerateColumns = true ;
GridView1.DataSource = getAllUserValuesfromBDC();
GridView1.DataBind();

}


public DataSet getAllUserValuesfromBDC()
{
string myvalues = string.Empty;
string fieldname = string.Empty;

DataSet ds = new DataSet();
try
{
DataTable mydt = new DataTable();

userinfo.UserProfileService myuser = new UserProfileService();
userinfo.PropertyData[] userprop = null;
userinfo.GetUserProfileByIndexResult myindx = null;

myuser.Credentials = System.Net.CredentialCache.DefaultCredentials;
long userCount = myuser.GetUserProfileCount();
int uCount = Convert.ToInt32(userCount);


// To add column to the data table
myindx = myuser.GetUserProfileByIndex(0);
if (myindx != null)
{
for (int m = 0; m < myindx.UserProfile.Length; m++)
{
try
{
if (myindx.UserProfile[m].Name != null)
{
mydt.Columns.Add(myindx.UserProfile[m].Name);
}
}
catch{}
}
}


// To add the Row value to Table
for (int k = 0; k < userCount; k++)
{
myindx = myuser.GetUserProfileByIndex(k);
DataRow dr = mydt.NewRow();

if (myindx != null)
{
for (int m = 0; m < myindx.UserProfile.Length; m++)
{
try
{
if (myindx.UserProfile[m].Name != null && myindx.UserProfile[m].Values[0].Value != null)
{
dr[myindx.UserProfile[m].Name] = myindx.UserProfile[m].Values[0].Value;
}
}
catch
{
}
}
mydt.Rows.Add(dr);
}
}
ds.Tables.Add(mydt);

}
catch
{

}
return ds;
}


Hope this will be more user full