zoukankan      html  css  js  c++  java
  • Coding获取站点中被任何用户标记为I like it的项

    最近,我做了一个在SharePoint2010中查询出所有被任何用户tag为I like it的content。在开发的过程中遇到两个问题:第一个是用户权限的提升,虽然模拟了SharePoint\system账号,但是创建的SocialTagManager对象的IsSocialAdmin属性始终是false.只有在User Profile Service Application中将当前用户设置为Administrator才查询出了所有数据。但是其他用户登录,不可能给其开放Administrator的权限。因此,要查询出所有的I Like it,还是必须从提升权限下手。遇到的第二个问题就是,如果查询出一个Uri被tag为I like it多少次?

    后来,经过研究前辈们的提升权限的方法。总算发现了怎么把当前上下文提升为管理员账号。并查询出一个Uri被tag为 I like it的次数。 我将实现的方法Share出来,希望对跟我遇到同样问题的朋友们能有所帮助。

    下面的代码是关于如何将当前上下文临时提升为管理员权限,注意HttpContext.Current必须在RunWithElevatedPrivileges的委托方法里面设置为NULL。

     1 SPSecurity.RunWithElevatedPrivileges(delegate
    2 {
    3 System.Web.HttpContext tempCtx = System.Web.HttpContext.Current;
    4 //emulate a system account
    5 using (SPSite site = new SPSite(SPContext.Current.Web.Site.Url, SPContext.Current.Web.Site.SystemAccount.UserToken))
    6 {
    7 //remove current user's context
    8 System.Web.HttpContext.Current = null;
    9 }
    10 }

      

    而对于第二个问题(查询I like it个数的),使用SocialTagManager.GetTerms()方法,传入要查询的Url,方法会返回所有给url上标记的SocialTerm。使用Linq找到I like it的SocialTerm。它有一个Count的属性,这个属性在MSDN上没有描述它的用途。通过测试,我发现它就是当前I like it被标记的个数。如果用户上下文没有提升到System Account的权限,那么找出来的值就始终是1或者0.

    下面是所有的相关的代码:

      1  public partial class SocialTaggedUrlListsWebPartUserControl : UserControl
    2 {
    3 /// <summary>
    4 /// Get or set a value that specifies the maximum number of the results
    5 /// </summary>
    6 public int Maxinum
    7 {
    8 get;
    9 set;
    10 }
    11
    12 /// <summary>
    13 /// Get or set a value that specifes the SocialTag Name which is used to search url.
    14 /// </summary>
    15 public string SocialTagName
    16 {
    17 get;
    18 set;
    19 }
    20
    21 private class TaggedItem
    22 {
    23 public TaggedItem(string title, string url, string iconUrl, int tagPeopleNum)
    24 {
    25 this.Title = title;
    26 this.Url = url;
    27 this.IconUrl = iconUrl;
    28 this.TagPeopleNum = tagPeopleNum;
    29 }
    30
    31 /// <summary>
    32 /// Get or set the Title property of TaggedItem object
    33 /// </summary>
    34 public String Title
    35 {
    36 get;
    37 set;
    38 }
    39
    40 /// <summary>
    41 /// Get or set the Url property of TaggedItem object
    42 /// </summary>
    43 public string Url
    44 {
    45 get;
    46 set;
    47 }
    48
    49 /// <summary>
    50 /// Get or set the IconUrl property
    51 /// </summary>
    52 public string IconUrl
    53 {
    54 get;
    55 set;
    56 }
    57
    58 /// <summary>
    59 /// Get or set the TagPeopleNum property ,which is used to specify the number of poeple who tagged the url with the specified SocialTag.
    60 /// </summary>
    61 public int TagPeopleNum
    62 {
    63 get;
    64 set;
    65 }
    66 }
    67
    68 protected void Page_Load(object sender, EventArgs e)
    69 {
    70
    71 }
    72
    73 protected override void Render(HtmlTextWriter writer)
    74 {
    75 base.Render(writer);
    76 SPSecurity.RunWithElevatedPrivileges(delegate
    77 {
    78 System.Web.HttpContext tempCtx = System.Web.HttpContext.Current;
    79 try
    80 {
    81 //emulate a system account
    82 using (SPSite site = new SPSite(SPContext.Current.Web.Site.Url, SPContext.Current.Web.Site.SystemAccount.UserToken))
    83 {
    84 string rootUrl = SPContext.Current.Site.Url;
    85
    86 //remove current user's context
    87 System.Web.HttpContext.Current = null;
    88
    89 SPServiceContext serverContext = SPServiceContext.GetContext(site);
    90 SocialTagManager socialTagManager = new SocialTagManager(serverContext);
    91 //get a term named 'I like it'
    92 TaxonomySession taxonomySession = new TaxonomySession(site);
    93 Term term = taxonomySession.GetTerms(SocialTagName, true).First();
    94 if (term != null)
    95 {
    96 //get an array of all urls owned by any user with the specified Term, and the scope is current site.
    97 SocialUrl[] socialUrls = FilterUrl(socialTagManager.GetAllUrls(term), rootUrl);
    98 if (socialUrls.Length == 0)
    99 {
    100 writer.Write("There are no available items tagged with \"" + this.SocialTagName + "\"");
    101 return;
    102 }
    103 using (SPWeb web = site.OpenWeb())
    104 {
    105 IList<TaggedItem> taggedItems = BuilderTaggedItem(web, socialTagManager, socialUrls, rootUrl);
    106
    107 if (taggedItems.Count == 0)
    108 {
    109 return;
    110 }
    111 //sort list by the Count property
    112
    113 writer.Write("<style>#topResultsTab td{ ");
    114 writer.Write(" height:32px; ");
    115 writer.Write(" border-bottom:1px dashed rgb(220,220,220); ");
    116 writer.Write("} </style>");
    117
    118 writer.Write(" <div class='item link-item' style='padding-left: 5px; border-bottom: 1px rgb(235,235,235) dashed;padding-right: 5px;'>");
    119
    120 writer.Write("<table id=\"topResultsTab\" style=\"100%\">");
    121
    122
    123 foreach (TaggedItem taggedItem in taggedItems)
    124 {
    125 writer.Write("<tr>");
    126
    127 writer.Write("<td style='18px; vertical-align: bottom; position: relative;'>");
    128 writer.Write(string.Format("<img src='{0}' width='16px' height='16px'/> ", GlobalVariables.SYS_IMG_RELATIVE_PATH + taggedItem.IconUrl));
    129 writer.Write("</td>");
    130
    131 writer.Write("<td style='font-weight: bold;font-size: 14px; vertical-align: bottom; position: relative;'> ");
    132 writer.Write(string.Format("<a style='font-size: 14px' href='{0}' >{1}</a>", taggedItem.Url, taggedItem.Title));
    133 writer.Write("</td>");
    134
    135 writer.Write("<td style='100px;vertical-align: bottom;color:#666; position: relative; text-align: left;'>");
    136 writer.Write(string.Format("<img src='{0}allusr.gif' alt='People' width='16px' height='16px' style='vertical-align: bottom;position: relative; '/>", GlobalVariables.SYS_IMG_RELATIVE_PATH));
    137 writer.Write(string.Format("<span >Tagged by <a href='javascript:;' class='people' msItemUrl='{0}'> {1} </a></span>", taggedItem.Url, taggedItem.TagPeopleNum));
    138 writer.Write("</td>");
    139
    140 writer.Write("</tr>");
    141 }
    142 }
    143 }
    144 else
    145 {
    146 writer.Write("There are no available items tagged with \"" + this.SocialTagName + "\"");
    147 }
    148 writer.Write("</table>");
    149 writer.Write("</div>");
    150 }
    151 }
    152 catch (Exception ex)
    153 {
    154 writer.Write(ex.Message);
    155 }
    156 finally
    157 {
    158 System.Web.HttpContext.Current = tempCtx;
    159 }
    160 });
    161 }
    162
    163
    164
    165 private SocialUrl[] FilterUrl(SocialUrl[] socialUrls, string filterUrl)
    166 {
    167 IList<SocialUrl> filteredUrls = new List<SocialUrl>();
    168 int index = 0;
    169 for (int i = 0; i < socialUrls.Length; i++)
    170 {
    171 if (socialUrls[i].Url.AbsoluteUri.StartsWith(filterUrl))
    172 {
    173 filteredUrls.Add(socialUrls[i]);
    174 index++;
    175 }
    176 }
    177 return filteredUrls.ToArray();
    178 }
    179
    180
    181 private IList<TaggedItem> BuilderTaggedItem(SPWeb web, SocialTagManager socialTagManager, SocialUrl[] socialUrls, string rootUrl)
    182 {
    183 //retrieve the number of terms associated with a specified url by the specified tag name .
    184 IList<object> socialTagList = new List<object>();
    185 for (int i = 0; i < socialUrls.Length; i++)
    186 {
    187 IList<SocialTerm> list = socialTagManager.GetTerms(socialUrls[i].Url, 100).Where(v => v.Term.Name.Equals(SocialTagName, StringComparison.CurrentCultureIgnoreCase)).ToList();
    188 socialTagList.Add(new { Url = socialUrls[i], Count = list.Count > 0 ? list[0].Count : 0, ModefiedData = socialUrls[i].LastModifiedTime });
    189 }
    190
    191 socialTagList = socialTagList.OrderByDescending(v => v.GetType().GetProperty("Count").GetValue(v, null)).ToList();
    192
    193 IList<TaggedItem> taggedItems = new List<TaggedItem>();
    194
    195 for (int i = 0; i < socialTagList.Count; i++)
    196 {
    197 TaggedItem taggedItem = null;
    198 object obj = null;
    199 string icon = string.Empty;
    200 SocialUrl socialUrl = socialTagList[i].GetType().GetProperty("Url").GetValue(socialTagList[i], null) as SocialUrl;
    201 int pepleNum = Convert.ToInt32(socialTagList[i].GetType().GetProperty("Count").GetValue(socialTagList[i], null));
    202          SPWeb correctWeb= GetWeb(web, socialUrl.Url.AbsoluteUri);
                       try
                       {
                        obj = correctWeb.GetObject(socialUrl.Url.AbsoluteUri);
                       }
                       catch
                       {
                           continue;
                       }
    216                 if (obj is SPListItem)
    217 {
    218 SPListItem spListItem = obj as SPListItem;
    219 icon = Microsoft.SharePoint.Utilities.SPUtility.MapToIcon(web, socialUrl.Title, spListItem.ProgId);
    220 taggedItem = new TaggedItem(spListItem.Title, socialUrl.Url.AbsoluteUri, icon, pepleNum);
    221 }
    222 else if (obj is SPFile)
    223 {
    224 SPFile spFile = obj as SPFile;
    225 taggedItem = new TaggedItem(socialUrl.Title, socialUrl.Url.AbsoluteUri, spFile.IconUrl, pepleNum);
    226 }
    227 else if (obj is SPFolder)
    228 {
    229 SPFolder spFolder = obj as SPFolder;
                          string title = string.IsNullOrEmpty(spFolder.Name) ? socialUrl.Title : spFolder.Name;
                          if ((socialUrl.Url.AbsoluteUri.EndsWith("/") ? socialUrl.Url.AbsoluteUri.Remove(socialUrl.Url.AbsoluteUri.Length - 1, 1) : socialUrl.Url.AbsoluteUri)
                            .Equals(
                            correctWeb.Url.EndsWith("/") ? correctWeb.Url.Remove(correctWeb.Url.Length - 1, 1) : correctWeb.Url))
                          {
                              title = string.IsNullOrEmpty(correctWeb.Title) ? "" : (correctWeb.Title+"-") + title;
                          }
                          icon = Microsoft.SharePoint.Utilities.SPUtility.MapToIcon(web, string.Empty, spFolder.ProgID);
                          taggedItem = new TaggedItem(title, socialUrl.Url.AbsoluteUri, "FOLDER.GIF", pepleNum);

    232 }
    233 else
    234 {
    235 taggedItem = new TaggedItem(socialUrl.Title, socialUrl.Url.AbsoluteUri, string.Empty, pepleNum);
    236
    237 }
    238 if (string.IsNullOrEmpty(taggedItem.IconUrl))
    239 {
    240 taggedItem.IconUrl = "icgen.gif";
    241 }
    242 taggedItems.Add(taggedItem);
    243 if (taggedItems.Count >= this.Maxinum)
    244 {
    245 break;
    246 }
    247 }
    248 return taggedItems;
    249 }
    250
    251 }

      

  • 相关阅读:
    android adb
    5 个免费的受欢迎的 SQLite 管理工具
    [Android]通过setImageURI设置网络上面的图片
    Android TextView实现长按复制文本功能的方法
    View工作原理(四)view的layout过程
    Anaroid WebView详解大全
    Android 如何在Eclipse中查看Android API源码以及support包源码
    关于Android的.so文件你所需要知道的
    AS问题解决系列3—iCCP: Not recognizing known sRGB profile(转)
    安卓App设计博文
  • 原文地址:https://www.cnblogs.com/snailJuan/p/2119822.html
Copyright © 2011-2022 走看看