kamagra how much to take

Error Handling In Office 365 And SharePoint Online

I am sure there is a lot of guidance out there regarding this subject. Personally, as things move more to a client-centric model I believe it is best to surface problems and issues with an application within a user exposable medium, such as a SharePoint list. This is possible to do within a SharePoint app in a relatively generic fashion. It makes it really easy to use. Firstly, consider the two following methods which act as the engines for capturing relevant errors:

  1. public TValue ErrorHandler<TValue>(Func<TValue> action)
  2. {
  3. try
  4. {
  5. return action();
  6. }
  7. catch (Exception ex)
  8. {
  9. DumpErrorIntoLog(DateTime.Now.ToString(CultureInfo.InvariantCulture), ex.Message);
  10. return default(TValue);
  11. }
  12. }
  13.  
  14. public void ErrorHandler(Action action)
  15. {
  16. try
  17. {
  18. action();
  19. }
  20. catch (Exception ex)
  21. {
  22. DumpErrorIntoLog(DateTime.Now.ToString(CultureInfo.InvariantCulture), ex.Message);
  23. }
  24. }

Using the above, handling the error is pretty easy. All you have to do is use the following method in order to harness an app level list, and dump the error in that. First though, to get the context token, web specs and all that crap I am going to use a context utility class.

  1. public class ContextUtility
  2. {
  3. public ContextUtility(HttpRequest request)
  4. {
  5. ContextDetails = new SpContext {ServerUrl = request.Url.Authority, HostWebUrl = HttpContext.Current.Request["SPHostUrl"], AppWebUrl = HttpContext.Current.Request["SPAppWebUrl"], ContextTokenString = TokenHelper.GetContextTokenFromRequest(request)};
  6.  
  7. if (ContextToken == null)
  8. {
  9. try
  10. {
  11. ContextToken = TokenHelper.ReadAndValidateContextToken(ContextDetails.ContextTokenString, ContextDetails.ServerUrl);
  12. }
  13. catch (Exception)
  14. {
  15. ContextToken = null;
  16. }
  17. }
  18. JavaScriptSerializer serializer = new JavaScriptSerializer();
  19. HttpCookie cookie = new HttpCookie("SPContext", serializer.Serialize(ContextDetails));
  20. cookie.Expires = DateTime.Now.AddHours(12);
  21. HttpContext.Current.Response.Cookies.Add(cookie);
  22. }
  23.  
  24. public ContextUtility(SpContext context)
  25. {
  26. ContextDetails = context;
  27. try
  28. {
  29. ContextToken = TokenHelper.ReadAndValidateContextToken(ContextDetails.ContextTokenString, ContextDetails.ServerUrl);
  30. }
  31. catch (Exception)
  32. {
  33. ContextToken = null;
  34. }
  35. }
  36.  
  37. public ContextUtility()
  38. {
  39. }
  40.  
  41. public SharePointContextToken ContextToken { get; set; }
  42. public SpContext ContextDetails { get; set; }
  43.  
  44. public bool IsValid
  45. {
  46. get { return ContextToken != null; }
  47. }
  48.  
  49. public static ContextUtility Current
  50. {
  51. get
  52. {
  53. ContextUtility spContext = null;
  54. if (HttpContext.Current.Request.Cookies["SPContext"] != null)
  55. {
  56. JavaScriptSerializer serializer = new JavaScriptSerializer();
  57. spContext = new ContextUtility((SpContext) serializer.Deserialize(HttpContext.Current.Request.Cookies["SPContext"].Value, typeof (SpContext)));
  58. }
  59. if (spContext == null || !spContext.IsValid)
  60. {
  61. spContext = new ContextUtility(HttpContext.Current.Request);
  62. }
  63.  
  64. if (spContext.IsValid)
  65. {
  66. return spContext;
  67. }
  68. HttpContext.Current.Response.Redirect(GetRedirectUrl());
  69. return null;
  70. }
  71. }
  72.  
  73. private static string GetRedirectUrl()
  74. {
  75. string hostWebUrl = HttpContext.Current.Request["SPHostUrl"];
  76. return TokenHelper.GetAppContextTokenRequestUrl(hostWebUrl, HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.Url.ToString()));
  77. }
  78. }
  79.  
  80. public class SpContext
  81. {
  82. public string HostWebUrl { get; set; }
  83. public string AppWebUrl { get; set; }
  84. public string ContextTokenString { get; set; }
  85. public string ServerUrl { get; set; }
  86. }

Then the actual SharePoint communication, and that’s it.

  1. public void DumpErrorIntoLog(string time, string message)
  2. {
  3. ContextUtility utility = new ContextUtility(HttpContext.Current.Request);
  4. using (ClientContext context = TokenHelper.GetClientContextWithContextToken(utility.ContextDetails.AppWebUrl, utility.ContextDetails.ContextTokenString, HttpContext.Current.Request.Url.Authority))
  5. {
  6. List list = context.Web.Lists.GetByTitle("ErrorsAndShit");
  7. context.Load(list);
  8. context.ExecuteQuery();
  9. if (list == null)
  10. {
  11. return;
  12. }
  13. CamlQuery query = CamlQuery.CreateAllItemsQuery();
  14. ListItemCollection items = list.GetItems(query);
  15. context.Load(items);
  16. context.ExecuteQuery();
  17. ListItem item = items.First();
  18. item["Errors"] = string.Format("Time: {0} Message: {1}", time, message);
  19. item.Update();
  20. context.ExecuteQuery();
  21. }
  22. }
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>