Advertisement
IAfanasov

Untitled

Jul 18th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.31 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Web;
  4. using System.Web.UI.WebControls;
  5. using System.Web.UI.WebControls.WebParts;
  6. using Microsoft.SharePoint;
  7. using System.Web.UI.HtmlControls;
  8. using System.Linq;
  9. using Microsoft.SharePoint.Administration;
  10.  
  11. namespace SubscribeProject.SubsWebPart
  12. {
  13.     [ToolboxItemAttribute(false)]
  14.     public class SubsWebPart : WebPart
  15.     {
  16.         #region Fields
  17.  
  18.         private string _linkText = "Подписаться на {0}";
  19.         private Guid _listId;
  20.         private string _succesText = "Вы успешно подписанны";
  21.         private string _errorSubscribeText = "Произошла ошибка подписки";
  22.         private string _cssClass = "subscribe-link";
  23.  
  24.         #endregion
  25.  
  26.         #region Properties
  27.         [Personalizable(PersonalizationScope.Shared)]
  28.         [WebBrowsable(true)]
  29.         [Category("Display")]
  30.         [WebDisplayName("Link Text")]
  31.         [WebDescription("Link Text")]
  32.         public string LinkText
  33.         {
  34.             get { return _linkText; }
  35.             set { _linkText = value; }
  36.         }
  37.         [Personalizable(PersonalizationScope.Shared)]
  38.         [WebBrowsable(true)]
  39.         [Category("Display")]
  40.         [WebDisplayName("List Id")]
  41.         [WebDescription("List Id")]
  42.         public Guid ListId
  43.         {
  44.             get { return _listId; }
  45.             set { _listId = value; }
  46.         }
  47.         [Personalizable(PersonalizationScope.Shared)]
  48.         [WebBrowsable(true)]
  49.         [Category("Display")]
  50.         [WebDisplayName("Succes text")]
  51.         [WebDescription("Succes sudscribe text")]
  52.         public string SuccessText
  53.         {
  54.             get { return _succesText; }
  55.             set { _succesText = value; }
  56.         }
  57.         [Personalizable(PersonalizationScope.Shared)]
  58.         [WebBrowsable(true)]
  59.         [Category("Display")]
  60.         [WebDisplayName("Error subscribe text")]
  61.         [WebDescription("")]
  62.         public string ErrorSubscribeText
  63.         {
  64.             get { return _errorSubscribeText; }
  65.             set { _errorSubscribeText = value; }
  66.         }
  67.         [Personalizable(PersonalizationScope.Shared)]
  68.         [WebBrowsable(true)]
  69.         [Category("Display")]
  70.         [WebDisplayName("CssClass")]
  71.         [WebDescription("")]
  72.         public string CustomizedCssClass
  73.         {
  74.             get { return _cssClass; }
  75.             set { _cssClass = value; }
  76.         }
  77.  
  78.  
  79.         #endregion
  80.  
  81.         #region Methods
  82.         protected override void CreateChildControls()
  83.         {
  84.             try
  85.             {
  86.                 if (SPContext.Current.Web.CurrentUser != null)
  87.                 {
  88.                     CssClass = CustomizedCssClass;
  89.                     HttpRequest request = HttpContext.Current.Request;
  90.  
  91.                     if (ListId == new Guid())
  92.                         if (SPContext.Current.List != null)
  93.                             ListId = SPContext.Current.ListId;
  94.                         else
  95.                         {
  96.                             Controls.Add(new HtmlAnchor { InnerText = "ListId is empty and current list is null" });
  97.                             return;
  98.                         }
  99.                     if (!SPContext.Current.Web.CurrentUser.Alerts.Cast<SPAlert>().Any(x => x.ListID == ListId))
  100.                         if (!string.IsNullOrEmpty(request.Form["subscriptionFlag"]) && request.Form["subscriptionFlag"] == "1")
  101.                             Subscribe();
  102.                         else
  103.                             if (HttpContext.Current.User.Identity.IsAuthenticated)
  104.                                 AddSubscriptionLink();
  105.                 }
  106.             }
  107.             catch (Exception exc)
  108.             {
  109.                 LogError(exc);
  110.                 Controls.Add(new HtmlAnchor { InnerText = ErrorSubscribeText });
  111.             }
  112.         }
  113.  
  114.         private void AddSubscriptionLink()
  115.         {
  116.             var htmlInputHidden = new Literal
  117.                                       {
  118.                                           Text =
  119.                                               "<input id=\"subscriptionFlag\" name=\"subscriptionFlag\" type=\"hidden\" value=\"0\">"
  120.                                       };
  121.             Controls.Add(htmlInputHidden);
  122.  
  123.             HtmlAnchor subscribeLink = new HtmlAnchor();
  124.             subscribeLink.InnerText = string.Format(LinkText,
  125.                                                     SPContext.Current.List == null
  126.                                                         ? ""
  127.                                                         : SPContext.Current.List.Title);
  128.             subscribeLink.HRef =
  129.                 "javascript:document.getElementById('subscriptionFlag').value='1';document.forms[0].submit()";
  130.             Controls.Add(subscribeLink);
  131.         }
  132.  
  133.         private void Subscribe()
  134.         {
  135.             SPUser user = SPContext.Current.Web.CurrentUser;
  136.             if (user == null)
  137.                 Controls.Add(new HtmlAnchor { InnerText = "Current user is null" });
  138.             else
  139.             {
  140.                 if (user.Alerts.Cast<SPAlert>().Any(x => x.ListID == ListId))
  141.                     Controls.Add(new HtmlAnchor { InnerText = "You already subscribed to this list" });
  142.                 else
  143.                 {
  144.                     try
  145.                     {
  146.                         user.Alerts.Add(SPContext.Current.Web.Lists[ListId], SPEventType.Add, SPAlertFrequency.Immediate,
  147.                                         SPAlertDeliveryChannels.Email);
  148.                         Controls.Add(new HtmlAnchor { InnerText = SuccessText });
  149.                     }
  150.                     catch (Exception exc)
  151.                     {
  152.                         LogError(exc);
  153.                         Controls.Add(new HtmlAnchor { InnerText = ErrorSubscribeText });
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.  
  159.         public static void LogError(Exception exc)
  160.         {
  161.             SPDiagnosticsCategory category = new SPDiagnosticsCategory("Subscribe link", TraceSeverity.High, EventSeverity.Error);
  162.             SPDiagnosticsService service = SPDiagnosticsService.Local;
  163.             service.WriteTrace(1, category, TraceSeverity.High, "SubscribeError: {0}, StackTrace: {1}", new { exc.Message, exc.StackTrace });
  164.         }
  165.         #endregion
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement