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

Thursday, August 30, 2007

How to Get Survey Data from Sharepoint

Get the Survey data of Site in sharepoint

SPWeb web = SPContext.Current.Web;

string parentUrl = SPContext.Current.Web.Site.RootWeb.Url;


string currentURL = Survey List Url;

SPSite site = new SPSite(currentURL);
SPWebCollection spcollection = site.AllWebs;
string inputsite = p_strUrl ;

SPListCollection myweblist

html += "

";
for (int k = 0; k < spcollection.Count; k++)
{
if (inputsite.ToLower().Trim() == spcollection.Names[k].ToLower().Trim())
{
myweblist = spcollection[k].Lists;
for (int i = 0; i < myweblist.Count; i++)
{
if (myweblist[i].BaseType.ToString() == "Survey")
{
Assignmentvalue = myweblist[i].Title;

string urlAssignmentvalue = myweblist[i].DefaultViewUrl ;

html += "";

}
}
}
}
html += "
>" + Assignmentvalue + "
";

How to Get Discussion Board Data from Share point

Get the data of DiscussionCategory using SPQuery


Get the SpWeb

SPWeb web = SPContext.Current.Web;

using ( web = site.OpenWeb())
{

SPQuery query = new SPQuery();

discussionBoardUrl is the url which points to Discussion Board Exist

SPList list = web.GetListFromUrl(discussionBoardUrl);


Query the SpList to get Item Collection


string qLess = string.Empty;
qLess +=
" ";


query.Query = qLess;


SPListItemCollection listCollection = list.GetItems(query);
string subject = string.Empty;
string category = string.Empty;
litDiscussion.ID = "discussionBoard";

Loop with Each ITem In List Collection

string html = "";

ArrayList arrCategory = new ArrayList();
foreach (SPListItem listItem in listCollection)
{
string lcategory = listItem.GetFormattedValue("DiscussionCategory");
if (arrCategory != null && listCollection.Count > 0 && !arrCategory.Contains(lcategory.ToLower().Trim()))
arrCategory.Add(lcategory.Trim());

}


html = "

";
html += "";


int count = 0;

for (int k = 0; k < arrCategory.Count; k++ )
{
category = arrCategory[k].ToString() ;
html += "

";


foreach (SPListItem listItem in listCollection)
{
string lcategory = listItem.GetFormattedValue("DiscussionCategory");

Arranging Category under that Discussion topics

if (arrCategory[k].ToString().ToLower().Trim() == lcategory.ToLower().Trim())
{
count = count + 1;
string last = listItem.GetFormattedValue("Last Updated");
subject = listItem.GetFormattedValue("Subject");
html += "

";


}

if (count >= displayCount)
{
break;
}
}
html += "";
if (count >= displayCount)
{
break;
}
}
html += "

Discussion Board

" + category + "

> " + subject + "
";
litDiscussion.Text = html;