Archive | February 2012

Duplicate grouping in SPGRIDVIEW

I recommend one to read this blog if he/she is working on spgridview control.

I had a requirement of grouping the records in the grid based on a column, grouping is straight forward and can be achieved with just few lines of code.However, this used to generate duplicate grouping headers for the records in the grid.

 oSpGridView.AllowGrouping = true;
 oSpGridView.GroupField = “Status”;
 oSpGridView.GroupFieldDisplayName = “Status”;

I had to sort the records by columns (that is used for grouping) in the caml query before it is bound to the grid control.This is the line of code that did the trick for me.

 SPQuery oSortQuery = new SPQuery();

//Sorting by status.
 oQuery.Query = “<FieldRef Name=’Status’ Ascending=’True’ />”;
 SPListItemCollection oColl = oList.GetItems(oSortQuery);

We can then use the collection object to bind it to the grid control.

Create site from site template in sharepoint 2010

I was trying to create a site programatically based on the template that i have designed , i thought this should be straight forward and  passed the site template name as a parameter for the creation. Unfortunately, this wasn’t successful as the code was unable to recognize the site template name.

This blog speaks how to pass site template name as parameter.

Query Active directory for Username

I have been working on sharepoint-sap integration off late and have faced several challenges. One of those that i would like to share is extracting the windows account name from the firstname and lastname that you get it from SAP.

The windows account that you get from , can then be used to add users to the site. Enclosed is the snippet .

string strAdPath = “LDAP://dc=yourcompanyname,dc=com”;
string strUN= @”yourusername”;
string strPWD= “yourpassword”;
DirectoryEntry oLastEntry;
DirectoryEntry oFirstEntry;

public static DirectoryEntry GetDirectory()
{
DirectoryEntry oEntry = new DirectoryEntry(strAdPath ,strUN, strPWD, AuthenticationTypes.Secure);
return oEntry;
}

//Pass firstname and lastname to get the username

public string GetUserName(string firstName, string lastName)
{
DirectoryEntry oDirectoryEntry = GetDirectory();

DirectorySearcher oSearch = new DirectorySearcher(oDirectoryEntry);

//Search Criteria based on firstname
oSearch.Filter = “(&(objectClass=user)(givenName=” + firstName + “))”;

//Find all the records matching firstname
SearchResultCollection oResult = oSearch.FindAll();

//Loop through each records
foreach (SearchResult r in oResult)
{
oFirstEntry = r.GetDirectory();

//Check if the lastname of records retrieved matches with the lastname supplied
if ((string)oFirstEntry.Properties[“sn”].Value == lastName)
{
oLastEntry = r.GetDirectoryEntry();
}
}

//Get the username
if (oLastEntry != null)
{

userName = (string)userInfo.Properties[“samaccountname”].Value ?? string.Empty;
}

return userName;
}