Advertisement
Guest User

SPAlert Issue

a guest
Dec 10th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.87 KB | None | 0 0
  1. public override void Execute(Guid targetInstanceId)
  2. {
  3.         try
  4.         {
  5.             EventLogger.LogInfo("Executing \"Notification Subscriber Timer Job\".\nDate & time of execution: " + DateTime.Now, "Notification Timerjob");
  6.  
  7.             var url = (string)Properties["Url"];
  8.  
  9.             if (string.IsNullOrEmpty(url))
  10.             {
  11.                 url = "https://sp-domain/";
  12.             }
  13.  
  14.             using (var site = new SPSite(url))
  15.             {
  16.                 var wbEnabled = site.RootWeb.Lists["SQTForum_EnabledDiscussions"];
  17.                 var exempted = site.RootWeb.Lists["ExemptedUsers"];
  18.                 var web = site.OpenWeb("/minside/netvaerksfora/");
  19.  
  20.                 if (!web.Exists)
  21.                 {
  22.                     EventLogger.LogError(
  23.                         "Web could not be found.\n Tried to get '/minside/netvaerksfora/'",
  24.                         "Notification Timerjob");
  25.                     return;
  26.                 }
  27.  
  28.                 foreach (SPWeb spWeb in web.Webs)
  29.                 {
  30.                     var discussion = TryGetList(spWeb, "Diskussionsliste");
  31.                     if (discussion == null)
  32.                     {
  33.                         EventLogger.LogError(
  34.                             "Diskussionsliste could not be found on " + spWeb.Url +
  35.                             "\nAction cancelled. Moving on to next web.",
  36.                             "Notification Timerjob");
  37.                         continue;
  38.                     }
  39.  
  40.                     var docLib = TryGetList(spWeb, "Delte dokumenter");
  41.                     if (docLib == null)
  42.                     {
  43.                         EventLogger.LogError(
  44.                             "Delte dokumenter could not be found on " +
  45.                             spWeb.Url +
  46.                             "\nAction cancelled. Moving on to next web.",
  47.                             "Notification Timerjob");
  48.                         continue;
  49.                     }
  50.  
  51.                     if (HasWaveBoard(wbEnabled, discussion.ID, spWeb.ID))
  52.                         continue;
  53.  
  54.                     CreateAlertsForUsersOnCurrentWeb(spWeb, exempted, discussion, docLib);
  55.  
  56.                     spWeb.Dispose();
  57.                 }
  58.             }
  59.  
  60.             EventLogger.LogInfo("\"Notification Subscriber Timer Job\" is done.\nDate & time of completion: " + DateTime.Now, "Notification Timerjob");
  61.         }
  62.         catch (Exception ex)
  63.         {
  64.             EventLogger.LogError(ex.Message, "Notification Timerjob", ex);
  65.         }
  66. }
  67.  
  68. // Checks if WaveBoard is active on current Web
  69. private static bool HasWaveBoard(SPList overview, Guid listId, Guid webId)
  70. {
  71.     return overview.Items.Cast<SPListItem>().Any(i => new Guid((string)i["SQTForum_WebID"]) == webId && new Guid((string)i["SQTForum_ListID"]) == listId && (bool)i["SQTForum_IsActive"]);
  72. }
  73.  
  74. // Iterates all users on current Web and creates alerts
  75. private static void CreateAlertsForUsersOnCurrentWeb(SPWeb web, SPList exempted, SPList discussion, SPList docLib)
  76. {
  77.     foreach (SPRoleAssignment role in web.RoleAssignments)
  78.     {
  79.         if (role.Member is SPUser)
  80.         {
  81.             var user = role.Member as SPUser;
  82.             CreateAlertsForUser(user, web, exempted, discussion, docLib);
  83.         }
  84.         if (!(role.Member is SPGroup)) continue;
  85.         var grp = role.Member as SPGroup;
  86.         foreach (SPUser user in grp.Users)
  87.         {
  88.             CreateAlertsForUser(user, web, exempted, discussion, docLib);
  89.         }
  90.     }
  91. }
  92.  
  93. // Checks if the user is valid and can get alerts created
  94. private static void CreateAlertsForUser(SPUser user, SPWeb web, SPList exempted, SPList discussion, SPList docLib)
  95. {
  96.     var alerts = web.Alerts;
  97.     var field = exempted.Fields.GetField("ExemptedUser");
  98.     var exemptedUser = exempted.Items.Cast<SPListItem>().SingleOrDefault(i => GetUserFromListItem(i, field.Id).LoginName == user.LoginName);
  99.  
  100.     if (string.IsNullOrEmpty(user.Email)) return;
  101.  
  102.     if (!alerts.Cast<SPAlert>().Any(a => a.ListID == discussion.ID && a.UserId == user.ID))
  103.     {
  104.         if (exemptedUser != null)
  105.         {
  106.             if (!(bool)exemptedUser["ExemptedDiscussion"])
  107.                 CreateAlert(user.Alerts.Add(), discussion);
  108.         }
  109.         else
  110.         {
  111.             CreateAlert(user.Alerts.Add(), discussion);
  112.         }
  113.     }
  114.  
  115.     if (alerts.Cast<SPAlert>().Any(a => a.ListID == docLib.ID && a.UserId == user.ID)) return;
  116.     if (exemptedUser != null)
  117.     {
  118.         if (!(bool)exemptedUser["ExemptedDocuments"])
  119.             CreateAlert(user.Alerts.Add(), docLib);
  120.     }
  121.     else
  122.     {
  123.         CreateAlert(user.Alerts.Add(), discussion);
  124.     }
  125. }
  126.  
  127. // Gets SPUser object from SPListItem
  128. public static SPUser GetUserFromListItem(SPListItem item, Guid fieldId)
  129. {
  130.     try
  131.     {
  132.         var field = (SPFieldUser)item.Fields[fieldId];
  133.  
  134.         if (field != null && item[fieldId] != null)
  135.         {
  136.             var fieldValue = (SPFieldUserValue)field.GetFieldValue(item[fieldId].ToString());
  137.             if (fieldValue != null)
  138.                 return fieldValue.User;
  139.         }
  140.     }
  141.     catch (Exception ex)
  142.     {
  143.         EventLogger.LogError(ex.Message, "Notification Timerjob", ex);
  144.     }
  145.     return null;
  146. }
  147.  
  148. // Creates alert
  149. private static void CreateAlert(SPAlert alert, SPList list)
  150. {
  151.     alert.Title = list.Title;
  152.     alert.AlertType = SPAlertType.List;
  153.     alert.List = list;
  154.     alert.AlertTemplate = list.AlertTemplate;
  155.     alert.Properties["eventtypeindex"] = "0"; // All = 0, Added = 1, Modified = 2, Deleted = 3, Discussion = 4
  156.     alert.Filter = string.Empty;
  157.     alert.AlertFrequency = SPAlertFrequency.Immediate;
  158.     alert.Update(false);
  159. }
  160.  
  161. // Iterates all lists on current Web and returns a list if a match is found
  162. private static SPList TryGetList(SPWeb web, string listname)
  163. {
  164.     return web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == listname);
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement