Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public override void Execute(Guid targetInstanceId)
- {
- try
- {
- EventLogger.LogInfo("Executing \"Notification Subscriber Timer Job\".\nDate & time of execution: " + DateTime.Now, "Notification Timerjob");
- var url = (string)Properties["Url"];
- if (string.IsNullOrEmpty(url))
- {
- url = "https://sp-domain/";
- }
- using (var site = new SPSite(url))
- {
- var wbEnabled = site.RootWeb.Lists["SQTForum_EnabledDiscussions"];
- var exempted = site.RootWeb.Lists["ExemptedUsers"];
- var web = site.OpenWeb("/minside/netvaerksfora/");
- if (!web.Exists)
- {
- EventLogger.LogError(
- "Web could not be found.\n Tried to get '/minside/netvaerksfora/'",
- "Notification Timerjob");
- return;
- }
- foreach (SPWeb spWeb in web.Webs)
- {
- var discussion = TryGetList(spWeb, "Diskussionsliste");
- if (discussion == null)
- {
- EventLogger.LogError(
- "Diskussionsliste could not be found on " + spWeb.Url +
- "\nAction cancelled. Moving on to next web.",
- "Notification Timerjob");
- continue;
- }
- var docLib = TryGetList(spWeb, "Delte dokumenter");
- if (docLib == null)
- {
- EventLogger.LogError(
- "Delte dokumenter could not be found on " +
- spWeb.Url +
- "\nAction cancelled. Moving on to next web.",
- "Notification Timerjob");
- continue;
- }
- if (HasWaveBoard(wbEnabled, discussion.ID, spWeb.ID))
- continue;
- CreateAlertsForUsersOnCurrentWeb(spWeb, exempted, discussion, docLib);
- spWeb.Dispose();
- }
- }
- EventLogger.LogInfo("\"Notification Subscriber Timer Job\" is done.\nDate & time of completion: " + DateTime.Now, "Notification Timerjob");
- }
- catch (Exception ex)
- {
- EventLogger.LogError(ex.Message, "Notification Timerjob", ex);
- }
- }
- // Checks if WaveBoard is active on current Web
- private static bool HasWaveBoard(SPList overview, Guid listId, Guid webId)
- {
- 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"]);
- }
- // Iterates all users on current Web and creates alerts
- private static void CreateAlertsForUsersOnCurrentWeb(SPWeb web, SPList exempted, SPList discussion, SPList docLib)
- {
- foreach (SPRoleAssignment role in web.RoleAssignments)
- {
- if (role.Member is SPUser)
- {
- var user = role.Member as SPUser;
- CreateAlertsForUser(user, web, exempted, discussion, docLib);
- }
- if (!(role.Member is SPGroup)) continue;
- var grp = role.Member as SPGroup;
- foreach (SPUser user in grp.Users)
- {
- CreateAlertsForUser(user, web, exempted, discussion, docLib);
- }
- }
- }
- // Checks if the user is valid and can get alerts created
- private static void CreateAlertsForUser(SPUser user, SPWeb web, SPList exempted, SPList discussion, SPList docLib)
- {
- var alerts = web.Alerts;
- var field = exempted.Fields.GetField("ExemptedUser");
- var exemptedUser = exempted.Items.Cast<SPListItem>().SingleOrDefault(i => GetUserFromListItem(i, field.Id).LoginName == user.LoginName);
- if (string.IsNullOrEmpty(user.Email)) return;
- if (!alerts.Cast<SPAlert>().Any(a => a.ListID == discussion.ID && a.UserId == user.ID))
- {
- if (exemptedUser != null)
- {
- if (!(bool)exemptedUser["ExemptedDiscussion"])
- CreateAlert(user.Alerts.Add(), discussion);
- }
- else
- {
- CreateAlert(user.Alerts.Add(), discussion);
- }
- }
- if (alerts.Cast<SPAlert>().Any(a => a.ListID == docLib.ID && a.UserId == user.ID)) return;
- if (exemptedUser != null)
- {
- if (!(bool)exemptedUser["ExemptedDocuments"])
- CreateAlert(user.Alerts.Add(), docLib);
- }
- else
- {
- CreateAlert(user.Alerts.Add(), discussion);
- }
- }
- // Gets SPUser object from SPListItem
- public static SPUser GetUserFromListItem(SPListItem item, Guid fieldId)
- {
- try
- {
- var field = (SPFieldUser)item.Fields[fieldId];
- if (field != null && item[fieldId] != null)
- {
- var fieldValue = (SPFieldUserValue)field.GetFieldValue(item[fieldId].ToString());
- if (fieldValue != null)
- return fieldValue.User;
- }
- }
- catch (Exception ex)
- {
- EventLogger.LogError(ex.Message, "Notification Timerjob", ex);
- }
- return null;
- }
- // Creates alert
- private static void CreateAlert(SPAlert alert, SPList list)
- {
- alert.Title = list.Title;
- alert.AlertType = SPAlertType.List;
- alert.List = list;
- alert.AlertTemplate = list.AlertTemplate;
- alert.Properties["eventtypeindex"] = "0"; // All = 0, Added = 1, Modified = 2, Deleted = 3, Discussion = 4
- alert.Filter = string.Empty;
- alert.AlertFrequency = SPAlertFrequency.Immediate;
- alert.Update(false);
- }
- // Iterates all lists on current Web and returns a list if a match is found
- private static SPList TryGetList(SPWeb web, string listname)
- {
- return web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == listname);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement