List Field added using InfoPath does not filter on [Me]
A Customer had created a custom InfoPath form to add a new item in the list. It gets added, but when he is trying to filter it using the field value =[Me], the filter does not work and all the items are shown to everyone.
Basically they are pulling in the items from User Information list from SharePoint and adding them to a drop down in InfoPath form, from where the user selects the user and the final item is added to their custom list.
But why filtering is not happening? When checking their list, found that they have used “Single line of text” as their field type to store the username !!
SharePoint does not know how to compare or validate a user inside a “Single line of text” field, so it just ignores the formula given in the View and displays all the records.
Now a very visible thing looks like we need to change the field type from “Single line of text” to “Person or Group” type which will allow filtering of data on [Me] filter. But on other hand, InfoPath does not know how to handle the “Person or Group” type field, so it is still sending out the plain text.
Then it was ascertained that we need an event handler which will sit in-between and capture each item being added. That event handler will pull in the username from the item being submitted and convert it to the SPUser format that “Person or Group” field understand.
So the code was written to get the data from InfoPath form and create a “Person or Group” field value from there on.
1: public override void ItemAdded(SPItemEventProperties properties)
2: {
3: using (SPSite site = new SPSite("https://myMOSSsite/"))
4: {
5: using (SPWeb web = site.OpenWeb())
6: {
7: SPList list = web.Lists[properties.ListId];
8: SPListItem item = list.Items[properties.ListItem.UniqueId];
9: string assignedTo = properties.ListItem["Assigned To"].ToString();
10: SPPrincipalInfo userInfo = SPUtility.ResolvePrincipal(web, assignedTo, SPPrincipalType.All, SPPrincipalSource.All, null, false);
11: SPFieldUserValue userValue = new SPFieldUserValue(web, userInfo.PrincipalId, userInfo.DisplayName);
12: item["Assigned"] = userValue;
13: item.Update();
14: }
15: }
16: }
Happy Coding and have fun…