Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ResourcesAttribute class

Derived from Object.Attribute and used for specifying how a property should be localized.

System.Object
  System.Attribute
    Microsoft.SharePoint.WebPartPages.ResourcesAttribute

Namespace:  Microsoft.SharePoint.WebPartPages
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class ResourcesAttribute : Attribute

At run time, the CustomPropertyToolPart looks for the ResourcesAttribute class. If found, the CustomPropertyToolPart iterates through the LoadResource method (you must override this method) with the resource values from the ResourcesAttribute. If the resource cannot be found, the CustomPropertyToolPart user interface is built with the default values. Otherwise, the CustomPropertyToolPart user interface is built with the localized values. The Microsoft .NET Framework will only provide localization values for the Property label, descriptions, and Category label. The base property name will not be localized. If the ResourcesAttribute class is not defined, the CustomPropertyToolPart user interface will revert back to the non-localized metadata (that is, CategoryAttribute, DescriptionAttribute, FriendlyNameAttribute, and the internal name of the property).

The following code example demonstrates how to localize your custom property when displayed in the CustomPropertyToolPart user interface.

//---------------------------------------------------------------------
//
// Code Summary: How to localize your custom property when displayed in the Property Grid. 
// FriendlyName, Description, and Category are localized.
//---------------------------------------------------------------------

using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint;
using System.Xml.Serialization;
using System.Collections.Specialized;
using System.Collections;
using System.Resources;
using System.Reflection;
using System.Drawing;
using System.Globalization;
using System.Runtime.InteropServices;

namespace WebControlLibrary4
{
    /// <summary>
    /// Summary description for WPLocalizedResource.
    /// </summary>
    [GuidAttribute("9ED54F84-A89D-4fcd-A854-44251E925C55")]
    [XmlRoot(Namespace="foo")]
    [ToolboxData("<{0}:WPLocalizedResource 
        runat=server></{0}:WPLocalizedResource>")]
    public class WPLocalizedResource : 
        Microsoft.SharePoint.WebPartPages.WebPart
    {

        const string c_MyText_Localized = "My Localized Property";
        private string _myText_Localized;

        public WPLocalizedResource() 
        {
            _myText_Localized = c_MyText_Localized;
        }

        //RESOURCES LOCALIZATION
        //Property that is localized. Use the ResourceAttibute.
        //[ResourcesAttribute (PropertyNameID=1, CategoryID=2, DescriptionID=3)]
        [ResourcesAttribute ("1","2","3")]
        [Category("Cool Properties")]
        [DefaultValue(c_MyText_Localized), 
            WebPartStorage(Storage.Personal)]
        [FriendlyNameAttribute("My Text Localized"), 
            Description("MyText Localized")]
        public string MyText_Localized
        {
            get
            {
                return _myText_Localized;
            }
            set
            {
                _myText_Localized = value;
            }
        }
        public override string LoadResource (string myID)
        {
            ResourceManager rm = new ResourceManager
                ("WebControlLibrary4.Resource1",
                Assembly.GetExecutingAssembly());
            return rm.GetString(myID);
        }
            
        //RENDER THE WEBPART
        protected override void RenderWebPart(HtmlTextWriter output)
        {
            output.Write("<br>Localized Resource Property Value: " + 
                this.MyText_Localized);
        }
    }
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2015 Microsoft