Problem
· We need to have an easy way to edit different text into the web site, for example some text into the header and footer. This problem appeared while working in a SharePoint 2010 project, as we wouldn’t use a webpart.
Solution
We designed two different solutions:
· In the first solution, we created a User web Control (.ascx) that has two parameters and retrieves both values from ShareProperty.
· For the second solution, we created a Custom Control inheriting the standard WebControl HyperLink. This solution has one parameter and retrieves the value from ShareProperty.
Using User web Control | Using Custom Control inheritance HyperLink | |
This solution uses a .ascx and both variables (text and url) to retrieve information dynamically. We created a user web control called HyperLink. | The problem with this solution is we need to add some configuration into the web.config and the text is not dynamic. | |
We need to add the flowing line into the web.config <system.web> <pages> <controls> <add tagPrefix="Controls" namespace="WebApplication1" assembly="WebApplication1" /> </controls> </pages> </system.web> | ||
Add the following register into the page <%@ Register TagPrefix="Controls" TagName="HyperLink" src="~/_controltemplates/ Branding/HyperLink.ascx" %> | Add the following register into the page <%@ Register tagPrefix="Controls" namespace="WebApplication1" %> | |
To use the web control you need to add the following sentence: <Controls:HyperLink Id="CanvasCampaign" KeyText="CanvasCampaignText" KeyUrl="CanvasCampaignUrl" runat="server"/> | To use the custom control you need to add the following sentence: <Controls:HyerLinkControl id="a1" runat="server" NavigateUrl=" CanvasCampaignUrl "> Link to read more about Canvas campaign </Controls:HyerLinkControl> | |
This code is added to the code behind: using System; using System.Collections; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; using System.ComponentModel; namespace Branding { public partial class HyperLink : UserControl { protected void Page_Load(object sender, EventArgs e) { string valueUrl = string.Empty; string valueText = string.Empty; Hashtable props = SPContext.Current.Web.Site.RootWeb.AllProperties; if (props.ContainsKey(KeyText)) { valueText = props[KeyText].ToString(); } if (props.ContainsKey(KeyUrl)) { valueUrl = props[KeyUrl].ToString(); } HyperLink1.Text = valueText; HyperLink1.NavigateUrl = valueUrl; } private string keyText=string.Empty; [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public string KeyText { get { return keyText; } set { keyText = value; } } private string keyUrl = string.Empty; [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public string KeyUrl { get { return keyUrl; } set { keyUrl = value; } } } } | This code is added to the code behind: using System; using System.Collections; using System.ComponentModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; namespace Branding { [DefaultProperty("Text")] [ToolboxData("<{0}:HyerLinkControl runat=server></{0}:HyerLinkControl>")] public class HyerLinkControl : System.Web.UI.WebControls.HyperLink { protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer) { string valueUrl = string.Empty; string valueText = string.Empty; Hashtable props = SPContext.Current.Web.Site.RootWeb.AllProperties; if (props.ContainsKey(this.NavigateUrl)) { valueUrl = props[this.NavigateUrl].ToString(); } this.NavigateUrl = valueUrl; base.AddAttributesToRender(writer); } } } | |
This code is added to the .aspx | ||
Open Sharepoint Designer Click on “Site Options” and add two new key-value pair. CanvasCampaignText = Link to read more about Canvas campaign CanvasCampaignUrl = http://www.... | Open Sharepoint Designer Click on “Site Options” and add new key-value pair. CanvasCampaignUrl = http://www.... | |
Refresh page, and in both solutions we have the same result: |
No hay comentarios:
Publicar un comentario