Answered by:
Retrieve user info CRM 2011 question

Question
-
Hi, we have several teams (for e.g. US North East, US West, US South) in crm 2011 online and users are assigned to specific team depending on their location/job role.
I need to identify user's team membership in one of the plug-in that I am writing. I can use the whoamiresponse to get the userid (assuming I register the plug-in in the context of "callinguser id").
How can I retrieve the user's team membership? Say if user A belongs to US North East, in the plug-in, how can I retrieve the user's
I need to perform this for e.g. "If user A belong to US North East, then perform some business logic" in the plug-in.
I can retrieve user's Org ID, BusinessUnitId etc., but I could not find User's Team Id. I searched at Teammembership but couldn't find.
Thanks for your help.
- Edited by CRM elite Tuesday, October 18, 2011 1:57 AM
Tuesday, October 18, 2011 12:37 AM
Answers
-
Where exactly its not working? You mentioned its working when you use WhoAmIRespose to get UserId. If you are doing this in Workflow or Plugin, you can extract UserId from WorkfloExectionContext or PlugInExecutionContext.
On LINQ, i dont think you need to have JOIN in LINQ, following code should be sufficient
bool usstaf = false; bool eurostaff = false; var siteqry = from u in svcContext.CreateQuery("systemuser") where u.GetAttributeValue<Guid>("systemuserid") == userid select u; foreach (var sitename in siteqry) { if (sitename.Attributes.Contains("siteid")) { if (sitename.GetAttributeValue<EntityReference>("siteid").name == "US Company Site") { usstaff = true; } if (sitename.GetAttributeValue<EntityReference>("siteid").name == "Euro Company Site") { eurostaff = true; } } }
MaKeer | myencounterwithcrm.wordpress.com | CRM2011 User Settings Utility | CRM2011 Lookup Preview- Marked as answer by CRM elite Thursday, October 20, 2011 9:13 PM
Thursday, October 20, 2011 7:06 PM
All replies
-
Use FetchExpression with entityname teammembership entity in SDK call.
If you wanted to use OrganizationServiceContex following LINQ can be used.
var query = from tm in _orgContext.CreateQuery("teammembership") join t in _orgContext.CreateQuery("team") on tm.GetAttributeValue<Guid>("teamid") equals t.GetAttributeValue<Guid>("teamid") where tm.GetAttributeValue<Guid>("systemuserid") == <<UserId>> select t;
HTH.
MaKeer | myencounterwithcrm.wordpress.com | CRM2011 User Settings Utility | CRM2011 Lookup Preview- Proposed as answer by Makarand Keer Tuesday, October 18, 2011 5:22 PM
Tuesday, October 18, 2011 3:23 AM -
Hi Makeer, thanks for your help. Appreciate it. Now, we will go by the site ID as supposed to teammership. But I can modify your query I suppose. Also what is the _orgContext.?
// Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); // Obtain the organization service reference. IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); OrganizationServiceContext svcContext = new OrganizationServiceContext(service);
Is it correct? But I couldn't find the AccountSet
Tuesday, October 18, 2011 4:53 PM -
orgContext is instance of OrganizationServiceContext.
In Plug-in you can get OSC as
OrganizationServiceContext OrgContext = new OrganizationServiceContext(service);
You will get AccountSet only if you are using Early Bound Classes. Without early bound classes use CreateQuery method of OCS to get Account set.
HTH
MaKeer | myencounterwithcrm.wordpress.com | CRM2011 User Settings Utility | CRM2011 Lookup Preview- Proposed as answer by Makarand Keer Tuesday, October 18, 2011 5:22 PM
Tuesday, October 18, 2011 5:22 PM -
Hi, Could you point me what I need to add to enable early bound.Tuesday, October 18, 2011 5:28 PM
-
Tuesday, October 18, 2011 5:36 PM
-
no, i would just need to enable proxytypes I believe.
- Edited by CRM elite Tuesday, October 18, 2011 5:42 PM
Tuesday, October 18, 2011 5:41 PM -
EnableProxyTypes required when you are using EarlyBound types, it will not create EarlyBound Types.
You have to use crmsvcutil to crate ProxyTypes and add it to Plugin project.
I prefer using late binding instead of Early binding, which does not require Proxy Types. For your query just use CreateQuery method and pass entity schema name as parameter.
HTH
MaKeer | myencounterwithcrm.wordpress.com | CRM2011 User Settings Utility | CRM2011 Lookup Preview- Proposed as answer by Makarand Keer Wednesday, October 19, 2011 10:09 PM
Tuesday, October 18, 2011 6:26 PM -
Hi Makarand, I tried to link systemuser with site entity to get the sitename, in the pre-operation of Create Entity Event. It didn't work; What am I missing here. I used the whoamIresponse to get the userid and it works. I need to valiate whether the logged on user is part of the US Site or Europe Site.
bool usstaf = false; bool eurostaff = false; var siteqry = from s in svcContext.CreateQuery("site") join u in svcContext.CreateQuery("systemuser") on s.GetAttributeValue<Guid>("siteid") equals u.GetAttributeValue<Guid>("siteid") where u.GetAttributeValue<Guid>("systemuserid") == userid select s; foreach (var sitename in siteqry) { if (sitename.Attributes.Contains("name")) { if (sitename.GetAttributeValue<string>("name") == "US Company Site") { usstaff = true; } if (sitename.GetAttributeValue<string>("name") == "Euro Company Site") { eurostaff = true; } } }
- Edited by CRM elite Thursday, October 20, 2011 6:33 PM
Thursday, October 20, 2011 6:32 PM -
Where exactly its not working? You mentioned its working when you use WhoAmIRespose to get UserId. If you are doing this in Workflow or Plugin, you can extract UserId from WorkfloExectionContext or PlugInExecutionContext.
On LINQ, i dont think you need to have JOIN in LINQ, following code should be sufficient
bool usstaf = false; bool eurostaff = false; var siteqry = from u in svcContext.CreateQuery("systemuser") where u.GetAttributeValue<Guid>("systemuserid") == userid select u; foreach (var sitename in siteqry) { if (sitename.Attributes.Contains("siteid")) { if (sitename.GetAttributeValue<EntityReference>("siteid").name == "US Company Site") { usstaff = true; } if (sitename.GetAttributeValue<EntityReference>("siteid").name == "Euro Company Site") { eurostaff = true; } } }
MaKeer | myencounterwithcrm.wordpress.com | CRM2011 User Settings Utility | CRM2011 Lookup Preview- Marked as answer by CRM elite Thursday, October 20, 2011 9:13 PM
Thursday, October 20, 2011 7:06 PM -
Ok. When I try to display the user last name - it displays as "SYSTEM". So it appears that the plug-in is running on admin context. I changed to run using context.initiatinguserid. But still the user lastname displays at "System". I register the plug-in using 'Calling User'
// Obtain the organization service reference. IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId); OrganizationServiceContext svcContext = new OrganizationServiceContext(service);
Thursday, October 20, 2011 8:55 PM