Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.Web;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using Microsoft.SharePoint;
- using System.Web.UI.HtmlControls;
- using System.Linq;
- using Microsoft.SharePoint.Administration;
- namespace SubscribeProject.SubsWebPart
- {
- [ToolboxItemAttribute(false)]
- public class SubsWebPart : WebPart
- {
- #region Fields
- private string _linkText = "Подписаться на {0}";
- private Guid _listId;
- private string _succesText = "Вы успешно подписанны";
- private string _errorSubscribeText = "Произошла ошибка подписки";
- private string _cssClass = "subscribe-link";
- #endregion
- #region Properties
- [Personalizable(PersonalizationScope.Shared)]
- [WebBrowsable(true)]
- [Category("Display")]
- [WebDisplayName("Link Text")]
- [WebDescription("Link Text")]
- public string LinkText
- {
- get { return _linkText; }
- set { _linkText = value; }
- }
- [Personalizable(PersonalizationScope.Shared)]
- [WebBrowsable(true)]
- [Category("Display")]
- [WebDisplayName("List Id")]
- [WebDescription("List Id")]
- public Guid ListId
- {
- get { return _listId; }
- set { _listId = value; }
- }
- [Personalizable(PersonalizationScope.Shared)]
- [WebBrowsable(true)]
- [Category("Display")]
- [WebDisplayName("Succes text")]
- [WebDescription("Succes sudscribe text")]
- public string SuccessText
- {
- get { return _succesText; }
- set { _succesText = value; }
- }
- [Personalizable(PersonalizationScope.Shared)]
- [WebBrowsable(true)]
- [Category("Display")]
- [WebDisplayName("Error subscribe text")]
- [WebDescription("")]
- public string ErrorSubscribeText
- {
- get { return _errorSubscribeText; }
- set { _errorSubscribeText = value; }
- }
- [Personalizable(PersonalizationScope.Shared)]
- [WebBrowsable(true)]
- [Category("Display")]
- [WebDisplayName("CssClass")]
- [WebDescription("")]
- public string CustomizedCssClass
- {
- get { return _cssClass; }
- set { _cssClass = value; }
- }
- #endregion
- #region Methods
- protected override void CreateChildControls()
- {
- try
- {
- if (SPContext.Current.Web.CurrentUser != null)
- {
- CssClass = CustomizedCssClass;
- HttpRequest request = HttpContext.Current.Request;
- if (ListId == new Guid())
- if (SPContext.Current.List != null)
- ListId = SPContext.Current.ListId;
- else
- {
- Controls.Add(new HtmlAnchor { InnerText = "ListId is empty and current list is null" });
- return;
- }
- if (!SPContext.Current.Web.CurrentUser.Alerts.Cast<SPAlert>().Any(x => x.ListID == ListId))
- if (!string.IsNullOrEmpty(request.Form["subscriptionFlag"]) && request.Form["subscriptionFlag"] == "1")
- Subscribe();
- else
- if (HttpContext.Current.User.Identity.IsAuthenticated)
- AddSubscriptionLink();
- }
- }
- catch (Exception exc)
- {
- LogError(exc);
- Controls.Add(new HtmlAnchor { InnerText = ErrorSubscribeText });
- }
- }
- private void AddSubscriptionLink()
- {
- var htmlInputHidden = new Literal
- {
- Text =
- "<input id=\"subscriptionFlag\" name=\"subscriptionFlag\" type=\"hidden\" value=\"0\">"
- };
- Controls.Add(htmlInputHidden);
- HtmlAnchor subscribeLink = new HtmlAnchor();
- subscribeLink.InnerText = string.Format(LinkText,
- SPContext.Current.List == null
- ? ""
- : SPContext.Current.List.Title);
- subscribeLink.HRef =
- "javascript:document.getElementById('subscriptionFlag').value='1';document.forms[0].submit()";
- Controls.Add(subscribeLink);
- }
- private void Subscribe()
- {
- SPUser user = SPContext.Current.Web.CurrentUser;
- if (user == null)
- Controls.Add(new HtmlAnchor { InnerText = "Current user is null" });
- else
- {
- if (user.Alerts.Cast<SPAlert>().Any(x => x.ListID == ListId))
- Controls.Add(new HtmlAnchor { InnerText = "You already subscribed to this list" });
- else
- {
- try
- {
- user.Alerts.Add(SPContext.Current.Web.Lists[ListId], SPEventType.Add, SPAlertFrequency.Immediate,
- SPAlertDeliveryChannels.Email);
- Controls.Add(new HtmlAnchor { InnerText = SuccessText });
- }
- catch (Exception exc)
- {
- LogError(exc);
- Controls.Add(new HtmlAnchor { InnerText = ErrorSubscribeText });
- }
- }
- }
- }
- public static void LogError(Exception exc)
- {
- SPDiagnosticsCategory category = new SPDiagnosticsCategory("Subscribe link", TraceSeverity.High, EventSeverity.Error);
- SPDiagnosticsService service = SPDiagnosticsService.Local;
- service.WriteTrace(1, category, TraceSeverity.High, "SubscribeError: {0}, StackTrace: {1}", new { exc.Message, exc.StackTrace });
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement