Friday, January 11, 2008

K2 Tips And Trick January 2008

In last few months, I encountered some common problem in K2 blackpearl development. I want to share what I've got:) I assume you are using Virtual PC given by K2 (DENALLIX)

Common problem #1: Get Active Directory's Full Name from Login Name
case : Suppose you want to get Codi's full name from active directory (login name : denallix\codi). You can use this code.

public string GetFullName(string userID)
{
DirectoryEntry entry = new DirectoryEntry("LDAP://DENALLIX.COM");
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(SAMAccountName=" + userID + ")";
searcher.PropertiesToLoad.Add("displayname");

SearchResult result = searcher.FindOne();

return result.Properties["displayname"][0].ToString();
}

Sample code : string myFullName = GetFullName("codi"); //returns Codi

Common problem #2: Get Active Directory's Email from Login Name
case : Suppose you want to get Codi's email from active directory (login name : denallix\codi).

public string GetEmail(string userID)
{
DirectoryEntry entry = new DirectoryEntry("LDAP://DENALLIX.COM");
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(SAMAccountName=" + userID + ")";
searcher.PropertiesToLoad.Add("mail");

SearchResult result = searcher.FindOne();

return result.Properties["mail"][0].ToString();
}
Sample code : string myFullName = GetEmail("codi"); //returns codi@denallix.com

Common problem #3: Insert a List Item to a Sharepoint List
case : Suppose you want to insert a list item to a Sharepoint List named "MyList". You can use this code.

public void InsertMyList(string Column1Value, string Column2Value, string Column3Value)
{
SPSite site = new SPSite("http://moss.denallix.com");
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPList myList = web.Lists["MyList"];
SPListItem myNewItem = myList.Items.Add();
myNewItem["Column1Title"] = Column1Value;
myNewItem["Column2Title"] = Column2Value;
myNewItem["Column3Title"] = Column3Value;
myNewItem.Update();
}
Sample code : InsertMyList("Column1Value","Column2Value","Column3Value");

Common problem #4: Get a List Item From a Sharepoint List with a condition (using CAML query)
case : Suppose you want to get Column2 Value from a list item where the Column1 Value is "Column1Value" . You can use this code.

public string GetColumn2ValueFromMyList(string Column1Value)
{
SPSite site = new SPSite("http://moss.denallix.com");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["MyList"];
SPQuery query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='Column1Title' /><Value Type='Text'>" + Column1Value + "</Value></Eq></Where>";
SPListItemCollection listItem = list.GetItems(query);
if (listItem.Count > 0)
{
return listItem[0]["Column2Title"].ToString();
}
else
{
return string.Empty;
}
}
sample code : string myColumn2Value = GetColumn2ValueFromMyList("Column1Value");
Some bonus from this week's tips and trick.
My friend had found a way to validate email format in Microsoft Infopath 2007 browser-enabled form. You can add a data validation matching this pattern:
.+@\p{L}+\.\p{L}+\.*.*

This pattern can handle agus123@yahoo.com , agus.santoso@yahoo.co.id , or agus_santoso@yahoo.co.id. I haven't tested it for all email format. Just inform me if you have better pattern. I will add it to this post. Thank you :)

2 comments:

chrisg said...

Great stuff, thanks for posting. I think you may want to look at the SharePoint list event wizard as it may help you with Item 3. Also play with the Search event wizard to do a search then use the returned results in your process that may be another way to help with number 4.

Also have you been to the Underground website?
www.k2underground.com

agoesz said...

wow, thanks for your comment :)
In my last project, I encountered a problem with memory leak. My module use a lot of activities in K2 project, resulting in memory leak problem (if you add a control to the K2 project, you cannot save the project).

This problem has been solved in K2 SP1. But my team cannot update it because the project is already up in production server. It's too risky updating to SP1. The workflow may be not worked again (I've been encountered this many times :)) ).

So to adjust my client's request, I edit the kprx file using notepad with the code in this blog. That's the only way to solve my problem (AFAIK).

FYI, my previous post was taken from K2 Underground's people too :)
Anyway, thank you for your suggestion.