1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.SharePoint;
6 using System.Diagnostics;
7 using System.Web;
8 using Microsoft.SharePoint.WebControls;
9 using System.IO;
10
11 namespace Cbw_SinopecPortal_WebPart
12 {
13 class SharePointUtility
14 {
15 public static int GetLookupID(object field)
16 {
17 string fieldValue = field.ToString();
18 return Convert.ToInt32(fieldValue.Substring(0, fieldValue.IndexOf(";#")));
19 }
20
21 public static string GetLookupLink(object field)
22 {
23 string fieldValue = Convert.ToString(field);
24 if (string.IsNullOrEmpty(fieldValue))
25 {
26 return null;
27 }
28 else
29 {
30 return fieldValue.Substring(0, fieldValue.IndexOf(", "));
31 }
32 }
33
34 public static SPList GetList(string listName)
35 {
36 listName = "Lists/" + listName;
37 foreach (SPList list in SPContext.Current.Web.Lists)
38 {
39 if (list.RootFolder.Url == listName)
40 {
41 return list;
42 }
43 }
44 throw new Exception("不存在指定的列表:" + listName);
45 }
46
47 public static SPList GetDocumentLibaray(string libarary)
48 {
49 foreach (SPList list in SPContext.Current.Web.Lists)
50 {
51 if (list.RootFolder.Url == libarary)
52 {
53 return list;
54 }
55 }
56 throw new Exception("不存在指定的文檔庫:" + libarary);
57 }
58
59 public static SPList GetListOrDocumentLibaray(string name)
60 {
61 string listName = "Lists/" + name;
62 foreach (SPList list in SPContext.Current.Web.Lists)
63 {
64 if (list.RootFolder.Url == listName || list.RootFolder.Url == name)
65 {
66 return list;
67 }
68 }
69 throw new Exception("不存在指定的列表OR文檔庫:" + name);
70 }
71
72 public static Dictionary<int, T> GetListItemDictionary<T>(string listName)
73 where T : Entity.Item, new()
74 {
75 var list = SharePointUtility.GetList(listName);
76 var items = from item in list.GetItems(new SPQuery()).Cast<SPListItem>()
77 select new { item.ID, Value = Entity.Item.Create<T>(item) };
78 return items.ToDictionary(item => item.ID, item => item.Value);
79 }
80
81 public static Dictionary<int, T> GetListItemDictionary<T>(string listName, SPQuery spquery)
82 where T : Entity.Item, new()
83 {
84 var list = SharePointUtility.GetList(listName);
85 var items = from item in list.GetItems(spquery).Cast<SPListItem>()
86 select new { item.ID, Value = Entity.Item.Create<T>(item) };
87 return items.ToDictionary(item => item.ID, item => item.Value);
88 }
89
90 public static Dictionary<int, T> GetDocumentItemDictionary<T>(string listName)
91 where T : Entity.Item, new()
92 {
93 var list = SharePointUtility.GetDocumentLibaray(listName);
94 var items = from item in list.GetItems(new SPQuery()).Cast<SPListItem>()
95 select new { item.ID, Value = Entity.Item.Create<T>(item) };
96 return items.ToDictionary(item => item.ID, item => item.Value);
97 }
98
99 public static Dictionary<int, T> GetFolderDictionary<T>(string listName)
100 where T : Entity.Item, new()
101 {
102 var list = SharePointUtility.GetDocumentLibaray(listName);
103 var items = from item in list.Folders.Cast<SPListItem>()
104 select new { item.ID, Value = Entity.Item.Create<T>(item) };
105 return items.ToDictionary(item => item.ID, item => item.Value);
106 }
107
108 public static T FindDocLibObj<T>(int ID, IEnumerable<T> Content)
109 where T : Entity.DocumentLibrary<T>, new()
110 {
111 var item = Content.Where(p => p.ID == ID).FirstOrDefault();
112 if (item != null)
113 {
114 return item;
115 }
116 foreach (var contentItem in Content.Where(p => p.IsFolder))
117 {
118 T result = FindDocLibObj(ID, contentItem.FolderCotent.Values);
119 if (result != null)
120 {
121 return result;
122 }
123 }
124 return null;
125 }
126
127 public static int GetTotalSubFileCountForCollection<T>(IEnumerable<T> Collection)
128 where T : Entity.DocumentLibrary<T>, new()
129 {
130 int TempSum = 0;
131 foreach (var item in Collection.Where(item => item.IsFolder))
132 {
133 TempSum += GetTotalSubFileCountForFolder(item);
134 }
135 return TempSum + Collection.Count(p => !p.IsFolder);
136 }
137
138 public static int GetTotalSubFileCountForFolder<T>(T DocLibObj)
139 where T : Entity.DocumentLibrary<T>, new()
140 {
141 int TempSum = 0;
142 foreach (var DocLibObjItem in DocLibObj.FolderCotent.Values.Where(p => p.IsFolder))
143 {
144 TempSum += GetTotalSubFileCountForFolder(DocLibObjItem);
145 }
146 return TempSum + DocLibObj.FolderCotent.Values.Count(p => !p.IsFolder);
147 }
148
149 public static string GetAttachmentUrl(SPAttachmentCollection Attachments)
150 {
151 if (Attachments != null && Attachments.Count > 0)
152 {
153 return Attachments.UrlPrefix + Attachments[0];
154 }
155 return null;
156 }
157
158 public static void WriteLog(Exception ex)
159 {
160 try
161 {
162 Debug.WriteLine(ex);
163 SPSecurity.RunWithElevatedPrivileges(delegate()
164 {
165 EventLog.WriteEntry("Cbw_SinopecPortal", ex.ToString());
166 });
167 }
168 catch
169 {
170
171 }
172 }
173
174 public static void PrivateLogin(string siteUrl)
175 {
176 SPSite site = new SPSite(siteUrl);
177 SPWeb web = site.OpenWeb();
178 HttpRequest httpRequest = new HttpRequest("", web.Url, "");
179 HttpContext.Current = new HttpContext(httpRequest, new HttpResponse(new StringWriter()));
180 SPControl.SetContextWeb(HttpContext.Current, web);
181 }
182 }
183 }