121 lines
3.3 KiB
C#
121 lines
3.3 KiB
C#
using Microsoft.SharePoint;
|
|
using System;
|
|
using System.Globalization;
|
|
using System.Xml;
|
|
namespace SPSolutions.SharePoint.Alerts
|
|
{
|
|
public class SPAlertUtil
|
|
{
|
|
public static string GetFilter(SPView view)
|
|
{
|
|
return SPAlertUtil.GetFilter(view.ParentList.ParentWeb, view);
|
|
}
|
|
public static SPUser GetUserNoThrow(SPAlert alert)
|
|
{
|
|
if (alert == null)
|
|
{
|
|
throw new ArgumentNullException("alert");
|
|
}
|
|
SPUser result;
|
|
try
|
|
{
|
|
result = alert.User;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
result = null;
|
|
}
|
|
return result;
|
|
}
|
|
public static SPList GetListNoThrow(SPAlert alert)
|
|
{
|
|
if (alert == null)
|
|
{
|
|
throw new ArgumentNullException("alert");
|
|
}
|
|
SPList result;
|
|
try
|
|
{
|
|
result = alert.List;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
result = null;
|
|
}
|
|
return result;
|
|
}
|
|
public static string GetFilter(SPWeb web, SPView view)
|
|
{
|
|
if (view == null || view.Query == null || view.Query.IndexOf("<DateRangesOverlap>") > 0)
|
|
{
|
|
return null;
|
|
}
|
|
return SPAlertUtil.GetFilter(web, view.Query);
|
|
}
|
|
public static string GetFilter(SPWeb web, string queryXml)
|
|
{
|
|
if (string.IsNullOrEmpty(queryXml))
|
|
{
|
|
return null;
|
|
}
|
|
XmlDocument xmlDocument = new XmlDocument();
|
|
xmlDocument.LoadXml("<Query>" + queryXml + "</Query>");
|
|
XmlNode xmlNode = xmlDocument.SelectSingleNode("Query/Where");
|
|
if (xmlNode == null)
|
|
{
|
|
return null;
|
|
}
|
|
if (!SPAlertUtil.IsValidFilter(web, xmlNode.InnerXml))
|
|
{
|
|
return null;
|
|
}
|
|
return string.Format("<Query>{0}</Query>", xmlNode.InnerXml);
|
|
}
|
|
public static string GetQuery(string queryXml)
|
|
{
|
|
if (string.IsNullOrEmpty(queryXml))
|
|
{
|
|
return null;
|
|
}
|
|
XmlDocument xmlDocument = new XmlDocument();
|
|
xmlDocument.LoadXml("<Query>" + queryXml + "</Query>");
|
|
XmlNode xmlNode = xmlDocument.SelectSingleNode("Query/Query");
|
|
if (xmlNode == null)
|
|
{
|
|
return null;
|
|
}
|
|
return string.Format("<Where>{0}</Where>", xmlNode.InnerXml);
|
|
}
|
|
public static bool IsValidFilter(SPWeb web, string filterXml)
|
|
{
|
|
SPRequestWrapper sPRequestWrapper = new SPRequestWrapper(web);
|
|
return sPRequestWrapper.ValidateSubscriptionFilter(filterXml);
|
|
}
|
|
public static void DeleteAlert(SPWeb web, SPAlert alert)
|
|
{
|
|
string listName = (alert.AlertType != SPAlertType.Custom) ? alert.List.ID.ToString("B").ToUpper(CultureInfo.InvariantCulture) : null;
|
|
int itemId = (alert.AlertType == SPAlertType.Item) ? alert.ItemID : 0;
|
|
int userId = (alert.User != null) ? alert.User.ID : 0;
|
|
SPRequestWrapper sPRequestWrapper = new SPRequestWrapper(web);
|
|
sPRequestWrapper.DeleteSubscription(web.Url, listName, alert.ID.ToString("D", CultureInfo.InvariantCulture), alert.AlertType == SPAlertType.Item, (uint)itemId, web.CurrentUser.IsSiteAdmin, userId);
|
|
}
|
|
public static bool IsValidDynamicRecipientField(SPField field)
|
|
{
|
|
return field != null && !field.Hidden && field is SPFieldUser;
|
|
}
|
|
public static void EnsureAlertProperty(SPAlert alert, string propertyName, string propertyValue)
|
|
{
|
|
if (alert.Properties.ContainsKey(propertyName))
|
|
{
|
|
alert.Properties[propertyName] = propertyValue;
|
|
return;
|
|
}
|
|
alert.Properties.Add(propertyName, propertyValue);
|
|
}
|
|
public static void EnsureAlertProperty(SPAlert alert, string propertyName, object propertyValue)
|
|
{
|
|
SPAlertUtil.EnsureAlertProperty(alert, propertyName, propertyValue.ToString());
|
|
}
|
|
}
|
|
}
|