About      |       Articles      |      Services      |      Software      |      Contact

Latest Free SharePoint Software

ARB Security Solutions regularly releases free SharePoint software, including WebParts, Client Applications, Framework Extensions, and other Miscellaneous Components.
The most recent freeware is:

Title: Simple SharePoint Rollup WebPart
Date Published: 10/22/2009

Previous Two Free WebPart Releases:

SecureCenter For SharePoint

By SharePoint security integrators, for SharePoint security integrators.

SharePoint Security Assurance Program™

For externally facing SharePoint deployments, security is an acutely important deployment concern. Learn how through daily security scanning, you can ensure external business users and partners that they can collaborate in confidence!

Security Assurance WebPart:



ToolPart Class With System.Reflection

If you are still working with some of the SharePoint base classes, you probably are familiar with the ToolPart class. The ToolPart class allows you optionally expose a bunch of neat little custom pieces you decide on regarding your WebPart in a natively expandable menu within the WebPart ToolPane. This is nice when doing something like displaying the copyright or whatever of your WebPart. The basic code for a ToolPart class looks like this:

C#:
  1. using Microsoft.SharePoint.WebPartPages;
  2. using System;
  3. using System.Web.UI;
  4.  
  5. namespace buenz.Webparts
  6. {
  7. public class BasicToolpart : ToolPart
  8. {
  9. public BasicToolpartMS()
  10. {
  11. base.Title = "My Name";
  12. }
  13.  
  14. public override void ApplyChanges()
  15. {
  16. }
  17.  
  18. public override void CancelChanges()
  19. {
  20. }
  21.  
  22. protected override void RenderToolPart(HtmlTextWriter output)
  23. {
  24. output.Write("Adam Buenz");
  25. }
  26.  
  27. public override void SyncChanges()
  28. {
  29. }
  30. }
  31. }

But, when you are displaying anything like version of WebParts etc. it is often a little bit eaiser to use .NET Reflection as opposed to having to go through a bunch of string values in the WebPart class file to display the version of the WebPart. Instead you can just pull it from the assignments made in the assembly info file using the System.Reflection namespace. To take advantage of this, there are just a couple things that you have to do. The first is to make a reference to System.Reflection and then initialize three strings to hold the data that is being pulled from the executing assembly, in this case the WebPart version, the Company name that built the webpart, and of course, the WebPart product name. The other is to get the information from the WebPart assembly file using GetExecutingAssembly() to get the location to the WebPart binary, and then get the relevant information.

C#:
  1. using Microsoft.SharePoint.WebPartPages;
  2. using System;
  3. using System.Web.UI;
  4. using System.Reflection;
  5.  
  6. namespace buenz.WebParts
  7. {
  8. public class ReflectingToolPart : ToolPart
  9. {
  10. private string labelVersion;
  11. private string labelCompanyName;
  12. private string labelProductName;
  13.  
  14. public ReflectingToolPart()
  15. {
  16. this.labelProductName = AssemblyProduct;
  17. this.labelCompanyName = AssemblyCompany;
  18. this.labelVersion = String.Format("Version {0}", AssemblyVersion);
  19. base.Title = "My WebPart";
  20. }
  21.  
  22. public override void ApplyChanges()
  23. {
  24. }
  25.  
  26. public string AssemblyVersion
  27. {
  28. get
  29. {
  30. return Assembly.GetExecutingAssembly().GetName().Version.ToString();
  31. }
  32. }
  33.  
  34. public string AssemblyProduct
  35. {
  36. get
  37. {
  38. // Get all Product attributes on this assembly
  39. object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
  40. // If there aren't any Product attributes, return an empty string
  41. if (attributes.Length == 0)
  42. return "";
  43. // If there is a Product attribute, return its value
  44. return ((AssemblyProductAttribute)attributes[0]).Product;
  45. }
  46. }
  47.  
  48. public string AssemblyCompany
  49. {
  50. get
  51. {
  52. // Get all Company attributes on this assembly
  53. object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
  54. // If there aren't any Company attributes, return an empty string
  55. if (attributes.Length == 0)
  56. return "";
  57. // If there is a Company attribute, return its value
  58. return ((AssemblyCompanyAttribute)attributes[0]).Company;
  59. }
  60. }
  61.  
  62. public override void CancelChanges()
  63. {
  64. }
  65.  
  66. protected override void RenderToolPart(HtmlTextWriter output)
  67. {
  68. output.Write("
  69. <center>");
  70. output.Write("
  71. ");
  72. output.Write(labelProductName);
  73. output.Write("");
  74. output.Write(labelVersion);
  75. output.Write("");
  76. output.Write(labelCompanyName);
  77. }public override void SyncChanges()
  78. {
  79. }
  80. }
  81. }

Just make sure that the attributes that you are pulling from the WebPart don't contain null values.

  • Share/Bookmark

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment