I am not able to update custom alert template on SharePoint list,Converts all subscribed or existing alerts of the list to the custom alert template.Update Existing/change custom alert template for SharePoint list.
below STSADM command works for SP2010 but not for 2013.
stsadm -o updatealerttemplates -url http://webapp/sitecol/ -filename "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\XML\custom_alerttemplates.xml"
Here is the solution:
Server side:
public static void AlertTemplate(SPWeb webElevated, SPList oList )
{
SPAlertTemplateCollection col = new SPAlertTemplateCollection(SPWebService.ContentService);
foreach (SPAlertTemplate spAlert in col)
{
//<AlertTemplate Type="List"
Default="True" Name ="My Template">
//above is based on your custom template
called custom_alerttemplates.xml
if (spAlert.Name == "My Template")
{
try
{
webElevated.AllowUnsafeUpdates
= true;
oList.AlertTemplate =
spAlert;
oList.Update();
webElevated.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
webElevated.AllowUnsafeUpdates = false;
}
break;
}
}
}
PowerShell:
add-pssnapin Microsoft.SharePoint.PowerShell -erroraction silentlycontinue
$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$AlertsTemplateCollection = new-object Microsoft.SharePoint.SPAlertTemplateCollection($contentService)
$spWeb = Get-SPWeb -Identity 'http://site/webUrL/'
$splists = $spWeb.lists
$alertslist = $splists["YourList"]
Write-Host $alertslist
$alertslist.AlertTemplate
$alertslist.AlertTemplate = $AlertsTemplateCollection["MyTemplate name based on .xml file"]
$alertslist.Update()
Write-Host $alertslist.AlertTemplate
$alertslist.AlertTemplate
$spWeb.Dispose()
Note: Once you execute above code, you should unsubscribe and subscribe alerts is mandatory - see the program below.
Complete Documented Notes:
Verify the “MyOrg_alerts.xml”
Go to the location of file system: C:\Program Files\Common
Files\microsoft shared\Web Server Extensions\15\TEMPLATE\XML and verify the
file name “MyOrg_alerts.xml” is available.
Register/add the “MyOrg_alerts” template into
SharePoint environment
STSADM command below:
stsadm -o updatealerttemplates -url "http://yourSiteUrl/"
-filename "C:\Program Files\Common Files\microsoft shared\Web Server
Extensions\15\TEMPLATE\XML\MyOrg_alerts.xml"
Note: The URL yellow
in color may vary based on the server, please change accordingly.
Update template to the library
Note: The URL yellow
in color may vary based on the server, please change accordingly.
add-pssnapin
Microsoft.SharePoint.PowerShell -erroraction silentlycontinue
$contentService
= [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$AlertsTemplateCollection
= new-object Microsoft.SharePoint.SPAlertTemplateCollection($contentService)
$spWeb =
Get-SPWeb -Identity 'http://yourSiteUrl/'
$splists =
$spWeb.lists
$alertslist =
$splists["MyOrg Events"]
Write-Host
$alertslist
Write-Host
$alertslist.AlertTemplate
$alertslist.AlertTemplate
= $AlertsTemplateCollection["MyOrg Template"]
$alertslist.Update()
Write-Host
$alertslist.AlertTemplate
$spWeb.Dispose()
End User /Subscriber Action:
Update If Already Existing users:
add-pssnapin Microsoft.SharePoint.PowerShell
-erroraction silentlycontinue
$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$AlertsTemplateCollection = new-object Microsoft.SharePoint.SPAlertTemplateCollection($contentService)
$spWeb = Get-SPWeb -Identity http://yourSiteUrl/
$splists = $spWeb.lists
$alertslist = $splists["MyOrg Events"]
$alerts = $spWeb.Alerts
if ($alerts.Count -gt 0)
{
$myalerts
= @()
foreach
($alert in
$alerts)
{
if(
$alert.ListID
-eq $alertslist.ID)
{
#
echo "Updating" + $alert.AlertTemplateName
$alert.AlertTemplate =
$AlertsTemplateCollection["MyOrg Template"]
$alert.Update()
# echo "Updated
$alert.AlertTemplateName"
}
}
}
$spWeb.Dispose()
public static void
ConvertsAllSubscribedUsersToNewlyAddedTemplate(SPWeb webElevated)
{ // Converts all
subscribed or existing alerts of the list to the custom alert template //
SPList lstCorporate = webElevated.Lists.TryGetList("MyOrg Events");
foreach (SPAlert
alert in webElevated.Alerts)
{
if (alert.ListID == lstCorporate.ID)
{
alert.AlertTemplate =
lstCorporate.AlertTemplate;
alert.Update();
}
}
}
No comments:
Post a Comment