PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/pbs2010/pbs2010/Layouts/pbs2010/ModifyPropertyBag.aspx.cs

#
C# | 297 lines | 264 code | 33 blank | 0 comment | 33 complexity | da26ddcc2fbf89023108fb44ceb20fe5 MD5 | raw file
  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using Microsoft.SharePoint;
  5. using Microsoft.SharePoint.WebControls;
  6. namespace Havivi.PropertyBagSettings
  7. {
  8. public class ModifyPropertyBag : PBSPageBase
  9. {
  10. protected Label lblKey;
  11. protected Label lblValue;
  12. protected Label lblHeader;
  13. protected Label lblMsg;
  14. protected TextBox txtKey;
  15. protected TextBox txtValue;
  16. protected LinkButton lnkSave1;
  17. protected LinkButton lnkSave2;
  18. protected ImageButton btnSave1;
  19. protected ImageButton btnSave2;
  20. protected CheckBox cbMask;
  21. private string scope = String.Empty;
  22. private Guid site = Guid.Empty;
  23. private Guid web = Guid.Empty;
  24. private Guid guid = Guid.Empty;
  25. private string key = String.Empty;
  26. private string mod = String.Empty;
  27. const string QS_SCOPE = "scope";
  28. const string QS_SITE = "site";
  29. const string QS_WEB = "web";
  30. const string QS_MODE = "mod";
  31. const string QS_KEY = "key";
  32. const string QS_GUID = "guid";
  33. const string EDIT_HEADER = "<H3 class='ms-standardheader'>Edit Property</H3>";
  34. const string DELETE_HEADER = "<H3 class='ms-standardheader'>Delete {0} Property</H3>";
  35. const string ADD_HEADER = "<H3 class='ms-standardheader'>Add Property</H3>";
  36. const string DEL_MSG = "Delete";
  37. const string DEL_IMG = "/_layouts/images/DELITEM.GIF";
  38. const string JS_MSGBOX = "return showMsg('{0}')";
  39. const string JS_CONFIRM = "return confirmModify('{0}','{1}')";
  40. const string MSG_DEL = "This will permanently delete the property! Please confirm that you want to delete the property ";
  41. const string MSG_ADD = "Please confirm that you want to add the property ";
  42. const string MSG_EDT = "Please confirm that you want to edit the property ";
  43. const string MSG_ADD_PRO = "The property was added ";
  44. const string MSG_DEL_PRO = "The property was deleted";
  45. const string MSG_EDT_PRO = "The property was modified ";
  46. protected override void OnLoad(EventArgs e)
  47. {
  48. base.OnLoad(e);
  49. if (Request.QueryString[QS_SITE] != null && Request.QueryString[QS_SITE] != String.Empty)
  50. {
  51. site = new Guid(Request.QueryString[QS_SITE]);
  52. }
  53. if (Request.QueryString[QS_WEB] != null && Request.QueryString[QS_WEB] != String.Empty)
  54. {
  55. web = new Guid(Request.QueryString[QS_WEB]);
  56. }
  57. if (Request.QueryString[QS_GUID] != null && Request.QueryString[QS_GUID] != String.Empty)
  58. {
  59. guid = new Guid(Request.QueryString[QS_GUID]);
  60. }
  61. if (Request.QueryString[QS_MODE] != null)
  62. {
  63. mod = Request.QueryString[QS_MODE];
  64. }
  65. if (Request.QueryString[QS_KEY] != null)
  66. {
  67. key = Request.QueryString[QS_KEY];
  68. }
  69. if (Request.QueryString[QS_SCOPE] != null)
  70. {
  71. scope = Request.QueryString[QS_SCOPE];
  72. }
  73. if (!Page.IsPostBack)
  74. {
  75. switch (mod)
  76. {
  77. case "edt":
  78. txtKey.Text = key;
  79. lblHeader.Text = EDIT_HEADER;
  80. txtKey.Enabled = false;
  81. LoadProp();
  82. lnkSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_EDT));
  83. lnkSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_EDT));
  84. btnSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_EDT));
  85. btnSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_EDT));
  86. break;
  87. case "del":
  88. txtKey.Text = key;
  89. lnkSave1.Text = DEL_MSG;
  90. lnkSave1.ToolTip = DEL_MSG;
  91. lnkSave2.Text = DEL_MSG;
  92. lnkSave2.ToolTip = DEL_MSG;
  93. btnSave1.ToolTip = DEL_MSG;
  94. btnSave2.ToolTip = DEL_MSG;
  95. btnSave1.ImageUrl = DEL_IMG;
  96. btnSave2.ImageUrl = DEL_IMG;
  97. lnkSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_DEL));
  98. lnkSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_DEL));
  99. btnSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_DEL));
  100. btnSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_DEL));
  101. txtKey.Visible = false;
  102. cbMask.Visible = false;
  103. txtValue.Visible = false;
  104. lblKey.Visible = false;
  105. lblValue.Visible = false;
  106. lblHeader.Text = String.Format(DELETE_HEADER, key);
  107. break;
  108. default:
  109. lnkSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_ADD));
  110. lnkSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_ADD));
  111. btnSave1.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_ADD));
  112. btnSave2.Attributes.Add("onclick", String.Format(JS_CONFIRM, key, MSG_ADD));
  113. lblHeader.Text = ADD_HEADER;
  114. break;
  115. }
  116. }
  117. }
  118. private void LoadProp()
  119. {
  120. switch (scope)
  121. {
  122. case "lst":
  123. using (SPSite oSPSite = new SPSite(site))
  124. {
  125. using (SPWeb oSPWeb = oSPSite.OpenWeb(web))
  126. {
  127. SPFolder oSPFolder = oSPWeb.GetFolder(guid);
  128. if (oSPFolder.Properties.ContainsKey(key))
  129. {
  130. txtValue.Text = oSPFolder.Properties[key].ToString();
  131. }
  132. }
  133. }
  134. break;
  135. default:
  136. using (SPSite oSPSite = new SPSite(site))
  137. {
  138. using (SPWeb oSPWeb = oSPSite.OpenWeb(web))
  139. {
  140. if (oSPWeb.AllProperties.ContainsKey(key))
  141. {
  142. txtValue.Text = oSPWeb.AllProperties[key].ToString();
  143. }
  144. }
  145. }
  146. break;
  147. }
  148. }
  149. private void Save()
  150. {
  151. try
  152. {
  153. string pbsValue = txtValue.Text;
  154. if (cbMask.Checked)
  155. {
  156. if (pbsValue != "" && txtKey.Text != "")
  157. {
  158. SPSecurity.RunWithElevatedPrivileges(delegate()
  159. {
  160. Havivi.PropertyBagSettings.cTripleDES des = new Havivi.PropertyBagSettings.cTripleDES();
  161. pbsValue = des.Encrypt(pbsValue);
  162. });
  163. }
  164. }
  165. switch (scope)
  166. {
  167. case "lst":
  168. using (SPSite oSPSite = new SPSite(site))
  169. {
  170. using (SPWeb oSPWeb = oSPSite.OpenWeb(web))
  171. {
  172. SPFolder oSPFolder = oSPWeb.GetFolder(guid);
  173. switch (mod)
  174. {
  175. case "edt":
  176. oSPFolder.Properties[txtKey.Text] = pbsValue;
  177. SPFolderUpdate(oSPWeb, oSPFolder);
  178. lblMsg.Text = MSG_EDT_PRO;
  179. break;
  180. case "del":
  181. oSPFolder.Properties[txtKey.Text] = null;
  182. oSPFolder.Properties.Remove(txtKey.Text);
  183. SPFolderUpdate(oSPWeb, oSPFolder);
  184. lblMsg.Text = MSG_DEL_PRO;
  185. break;
  186. default:
  187. oSPFolder.Properties.Add(txtKey.Text, pbsValue);
  188. SPFolderUpdate(oSPWeb, oSPFolder);
  189. lblMsg.Text = MSG_ADD_PRO;
  190. break;
  191. }
  192. }
  193. }
  194. break;
  195. default:
  196. using (SPSite oSPSite = new SPSite(site))
  197. {
  198. using (SPWeb oSPWeb = oSPSite.OpenWeb(web))
  199. {
  200. switch (mod)
  201. {
  202. case "edt":
  203. oSPWeb.AllProperties[txtKey.Text] = pbsValue;
  204. SPWebUpdate(oSPWeb);
  205. lblMsg.Text = MSG_EDT_PRO;
  206. break;
  207. case "del":
  208. oSPWeb.AllProperties[txtKey.Text] = null;
  209. oSPWeb.AllProperties.Remove(txtKey.Text);
  210. SPWebUpdate(oSPWeb);
  211. lblMsg.Text = MSG_DEL_PRO;
  212. break;
  213. default:
  214. oSPWeb.AllProperties.Add(txtKey.Text, pbsValue);
  215. SPWebUpdate(oSPWeb);
  216. lblMsg.Text = MSG_ADD_PRO;
  217. break;
  218. }
  219. }
  220. }
  221. break;
  222. }
  223. }
  224. catch (Exception ex)
  225. {
  226. lblMsg.Text = ex.Message;
  227. }
  228. }
  229. protected void Save_Click(object sender, ImageClickEventArgs e)
  230. {
  231. Save();
  232. }
  233. protected void Save_Click(object sender, EventArgs e)
  234. {
  235. Save();
  236. }
  237. private void SPWebUpdate(SPWeb oSPWeb)
  238. {
  239. if (!oSPWeb.AllowUnsafeUpdates)
  240. {
  241. oSPWeb.AllowUnsafeUpdates = true;
  242. oSPWeb.Update();
  243. oSPWeb.AllowUnsafeUpdates = false;
  244. }
  245. else
  246. {
  247. oSPWeb.Update();
  248. }
  249. }
  250. private void SPFolderUpdate(SPWeb oSPWeb, SPFolder oSPFolder)
  251. {
  252. if (!oSPWeb.AllowUnsafeUpdates)
  253. {
  254. oSPWeb.AllowUnsafeUpdates = true;
  255. oSPFolder.Update();
  256. oSPWeb.AllowUnsafeUpdates = false;
  257. }
  258. else
  259. {
  260. oSPFolder.Update();
  261. }
  262. }
  263. }
  264. }