kamagra how much to take

Free SharePoint Redirection WebPart Programming SharePoint WebParts With LINQ to Active Directory Part 2

In the first part of this series, I illustrated the initial building of the primary WebPart class and the workhorse method that taps into Active Directory using LINQ to Active Directory to retrieve the current user’s department. Now, we have to use the returned department string to query down into a helper redirection SharePoint list and use a LINQ query to compare the two strings. We can grab the corresponding value out of the URL column, and they using standard redirection techniques push the user to the destination.

The PushUserToDivisionSite method is responsible for the actual redirection of the user. Firstly, a new SPSite & SPWeb object will be instantiated using the Url property, then a SPList constructed using the List property. To build the Where construct a SPList.Items.Cast<SPListItem>().Where is used performing an Equals on the department of the user and the value of the Title column. This should actually be a String.Compare so that you could bypass any case sensitivity problems, but I am just noticing that now. So you might want to fix that. An Enumerable.FirstOrDefault is used to return the desired SPListItem, and the URL value brought out. This value is then used in a trivial Page.Response.Redirect.

  1. private void PushUserToDivisionSite()
  2. {
  3. using (var site = new SPSite(Url))
  4. {
  5. using (SPWeb web = site.OpenWeb())
  6. {
  7. SPList list = web.Lists[List];
  8. var results = list.Items.Cast().Where(item =&gt; Equals(item["Title"].ToString(), GetCurrentUserDepartment()));
  9. if (results.Count() &gt; 0)
  10. {
  11. SPListItem hydratedItem = results.FirstOrDefault();
  12. string url = hydratedItem[UrlProperty].ToString();
  13. if (!string.IsNullOrEmpty(url))
  14. {
  15. Page.Response.Redirect(url, true);
  16. }
  17. else
  18. {
  19. HandleException(new Exception("Redirection Did Not Occur. Please Check The URL Property Of The List Being Used."));
  20. }
  21. }
  22. }
  23. }
  24. }

The CreateChildControls method of the WebPart doesn’t do much besides call relevant helper methods. Since the GetCurrentUserDepartment method will either return the department or a constant string value as specified in the global fields, that can simplify be leveraged in a switch. So, either the department is returned, or the constant matches and a new Exception is thrown using the HandleException method which doesn’t do much but emit a literal control.

  1. protected override void CreateChildControls()
  2. {
  3. if (!_error)
  4. {
  5.  
  6. try
  7. {
  8. base.CreateChildControls();
  9. string department = GetCurrentUserDepartment();
  10. switch (department)
  11. {
  12. case _breakStatement:
  13. HandleException(new Exception("It Appears Your Active Directory Account Has No Department Set!"));
  14. break;
  15. default:
  16. PushUserToDivisionSite();
  17. break;
  18. }
  19. }
  20. catch (Exception ex)
  21. {
  22. HandleException(ex);
  23. }
  24. }
  25. }

And that’s it. So going through this again, I think there are two big things that LINQ to Active Directory provides that are crucial to recognize, regardless of whether you like the actual implementation of the concept.

  1. Providing a layer of abstraction over Active Directory functions promotes standardization and ease of use.
  2. Being able to use LINQ querying constructs is extremely helpful when trying to keep code succinct and providing homogenous querying mechanisms.
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>