I am trying a way to get all alerts from all sites from a SPUser in a summarized email. Is there any way to achieve this or I have to fetch each SPSite individually?
Thank you very much
|
I am trying a way to get all alerts from all sites from a SPUser in a summarized email. Is there any way to achieve this or I have to fetch each SPSite individually? Thank you very much |
|||
|
|
|
You need to simply go through site, iterate through each web within the web loop through each alert, then check to see if its the same user you only want, print to string or what ever you want :)
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spalertcollection(v=office.12).aspx to send an email create a seperate method that you call after the loop has finished using the SPUtility method: How to send E-Mail using SPUtility.SendEmail http://www.sharepointdiary.com/2012/01/how-to-send-e-mail-using.html so it will look somthing similar to this:
im sure ive propbably missed somthing out or done somthing wrong as im doing it all from memory and cant test it what so ever so its a proof of consept ;) but im sure there are some ways of probably doing it better but thats the first thing that has come to my mind :) EDIT the method above is very slow as it has alot of looping todo and will depend on how your doing it, for me I created this on a application as I can just show on another thread a loading screen(form) whilst it loops through and finishes :) but if you cant wait or dont want to do that or not in an app than i would recommend you call the database that holds the values! more specifically your looking for the table schedSubscription within that it will contain all the alerts, you can query it by using this:
have that in a stored procedure that you can call from a webpart or application or call directly (not recommended due to sql attacks), have a paramiter for user email to query against. when you call the stored procedure pass the value of the user email to return the results for the user only! from that you should get all the info you want from list name to site name and everything else :), you can then display it in a list or what ever you want! This method is very very fast and a better option ;) ahh btw make sure the stored procedure only has read access and nothing more! EDIT
open sql server manager and open your DB the personal one not sharepoint DB. goto programmability folder and then stored procedures. rightclick stored procedure folder and select "new stored procedure". copy paste code from above instead of the generic code. ok there is only two places you need to change, at the start change [DBNameGoesHere] to the DB that you store your common stored procedures and tables NOT the sharepoint DB is stored!!! now goto from and change WSS_Content to the DB name to whatever its call that contains the SchedSubscriptions table to query. Once you have done that you can compile and execute the code, you should see the new stored procedure within the Programmability folder under stored procedures, refresh the folder if you cant! last step isto give permissions to that stored procedure (read only). You can now test the stored procedure by right clicking it and select "execute stored procedure", you should get a box with one input.... on the right hand side under "value" insert the email you want to query and click ok. You should see a list of alerts to only that user! call this stored procedure from whereever you feel like be it from webpart or application ;)! if you dont want all columns returned and only the ones you want just change
to
so it will look like this
that will only return thoes columns :) to call a stored procedure follow: http://stackoverflow.com/questions/10556354/call-stored-procedure-in-c-sharp EDIT You dont need to replicate the stored procedure on each DB for each web application, just ammend the current stored procedure to take in the new DB tables like so:
this is getting from 4 different content DB so shows all from all web applications. This is the only method that i know of that is very fast as DB calls are extreamly fast compared to sharepoint (looping through spsite and spweb), if you have a dataaccess layer to call the stored procedure than its even more secure to call the stored procedure :),
I dont currently know if that is possible to have a master alert that alerts the user from all spsites. I would presume that involves connecting splist to one master list that contains an alert but it would be difficult. The solution i propose it relativly easy, 1 stored procedure that is connected to by webpart or application or list or feature that returns the result ;), as you stated you want to email them, I would go for the webpart method but you can setup a custom list that gets populated by the stored procedure with those values. The list would be created in a webpart and populated through that. This would also mean its only visible to the current user so only them can see their own alerts (all of them). hope it helps :) |
|||||||||||
|
|
You can also get User's alerts through Powershell : try this :
also be recursively call subwebs using Hope it helps!!! |
|||
|