Common problem #1: Get Active Directory's Full Name from Login Name
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
Sample code : string myFullName = GetEmail("codi"); //returns codi@denallix.com
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();
}
Common problem #3: Insert a List Item to a Sharepoint List
Sample code : InsertMyList("Column1Value","Column2Value","Column3Value");
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();
}
Common problem #4: Get a List Item From a Sharepoint List with a condition (using CAML query)
sample code : string myColumn2Value = GetColumn2ValueFromMyList("Column1Value");
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;
}
}
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:
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
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.
Post a Comment