设为首页 收藏本站
查看: 565|回复: 0

[经验分享] [SharePoint 2010] 自定义字段类型开发(二)

[复制链接]
累计签到:4 天
连续签到:1 天
发表于 2015-9-27 11:42:21 | 显示全部楼层 |阅读模式
  在SharePoint 2010中实现View Action Button效果。
  http://www.sharepointblogs.be/blogs/vandest/archive/2008/06/20/view-action-button.aspx
  1. 创建自定义字段类ViewActionButton 继承自 SPTextField


DSC0000.gif DSC0001.gif


    public class ViewActionButton : SPField
{
#region Constructors
public ViewActionButton(SPFieldCollection fields, string fieldName)
: base(fields, fieldName) { }
public ViewActionButton(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName) { }
#endregion
#region Reimplementation of Get/Set Custom Property
public new void SetCustomProperty(string propertyName, object propertyValue)
{
Type type = typeof(SPField);
MethodInfo setField = type.GetMethod("SetFieldAttributeValue", BindingFlags.NonPublic | BindingFlags.Instance);
object o = setField.Invoke(this, new object[] { propertyName, propertyValue.ToString() });
}
public new string GetCustomProperty(string propertyName)
{
Type type = typeof(SPField);
MethodInfo getField = type.GetMethod("GetFieldAttributeValue", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(String) }, null);
object o = getField.Invoke(this, new object[] { propertyName });
return o as String;
}
#endregion
}
View Code  2. 创建字段属性用户控件ActionButtonPropertyControl
  ActionButtonPropertyControl.ascx





<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Register TagPrefix="wssuc" TagName="InputFormControl" src="~/_controltemplates/InputFormControl.ascx" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ActionButtonPropertyControl.ascx.cs" Inherits="CustomFieldProject.ControlTemplates.ViewActionButton.ActionButtonPropertyControl" %>
<wssuc:InputFormControl runat="server" LabelText="Action URL:">
<Template_Control>
<span style="white-space: nowrap;">
<SharePoint:ProjectProperty runat="server" Property="Url" />/
<SharePoint:InputFormTextBox ID="iftxtUrl" runat="server" CssClass="ms-input" TextMode="SingleLine" Width="200" />
<asp:RequiredFieldValidator ID="rfvUrl" runat="server" ControlToValidate="iftxtUrl" Display="Dynamic" ErrorMessage="*" />
</span>
</Template_Control>
</wssuc:InputFormControl>
<wssuc:InputFormControl runat="server" LabelText="Format:">
<Template_Control>
<asp:DropDownList ID="ifrbFormat" runat="server">
<asp:ListItem Selected="True" Value="Button" Text="Button" />
<asp:ListItem Selected="False" Value="Hyperlink" Text="Hyperlink" />
</asp:DropDownList>
</Template_Control>
</wssuc:InputFormControl>
<wssuc:InputFormControl runat="server" LabelText="CSS-Class:">
<Template_Control>
<SharePoint:InputFormTextBox ID="iftxtCssClass" runat="server" CssClass="ms-input" TextMode="SingleLine" Text="ms-ButtonHeightWidth" />
<asp:RequiredFieldValidator ID="rfvCssClass" runat="server" ControlToValidate="iftxtCssClass" Display="Dynamic" ErrorMessage="*" />
</Template_Control>
</wssuc:InputFormControl>
View Code   请根据实际情况修改下列内容:



<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ActionButtonPropertyControl.ascx.cs" Inherits="CustomFieldProject.ControlTemplates.ViewActionButton.ActionButtonPropertyControl" %>
  ActionButtonPropertyControl.ascx.cs





    public partial class ActionButtonPropertyControl : UserControl, IFieldEditor
{
#region Controls and Properties to get/set value of Controls
// protected InputFormTextBox iftxtUrl;
public string ActionURL
{
get { return iftxtUrl.Text; }
set { iftxtUrl.Text = value; }
}
//protected DropDownList ifrbFormat;
public string Format
{
get { return ifrbFormat.SelectedValue; }
set { ifrbFormat.SelectedValue = value; }
}
//protected InputFormTextBox iftxtCssClass;
public string CssClass
{
get { return iftxtCssClass.Text; }
set { iftxtCssClass.Text = value; }
}
#endregion
#region IFieldEditor Members
public bool DisplayAsNewSection
{
get { return false; }
}
public void InitializeWithField(SPField field)
{
// If it's no postback (first time loading on Edit Field)
if (!IsPostBack)
{
CustomFieldTypes.ViewActionButton myField = field as CustomFieldTypes.ViewActionButton;
if (null != myField)
{
// Set values in control based on values in myField
Type type = typeof(ActionButtonPropertyControl);
foreach (PropertyInfo prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
if (prop.CanWrite)
prop.SetValue(this, myField.GetCustomProperty(prop.Name), null);
}
}
}
}
public void OnSaveChange(SPField field, bool isNewField)
{
CustomFieldTypes.ViewActionButton myField = field as CustomFieldTypes.ViewActionButton;
if (null != myField)
{
// Set values in myField based on values in control
Type type = typeof(ActionButtonPropertyControl);
foreach (PropertyInfo prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
if (prop.CanWrite)
myField.SetCustomProperty(prop.Name, prop.GetValue(this, null));
}
}
}
#endregion
}
View Code  3. 创建自定义字段定义文件fldtypes_ViewActionButton





<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">ViewActionButton</Field>
<Field Name="TypeDisplayName">View Action Button</Field>
<Field Name="TypeShortDescription">View Action Button</Field>
<Field Name="ParentType">Text</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="ShowOnListCreate">TRUE</Field>
<Field Name="ShowOnSurveyCreate">TRUE</Field>
<Field Name="ShowOnDocumentLibraryCreate">TRUE</Field>
<Field Name="ShowOnColumnTemplateCreate">TRUE</Field>
<Field Name="Sortable">FALSE</Field>
<Field Name="Filterable">FALSE</Field>
<Field Name="CAMLRendering">TRUE</Field>
<Field Name="FieldEditorUserControl">/_controltemplates/ViewActionButton/ActionButtonPropertyControl.ascx</Field>
<Field Name="FieldTypeClass">CustomFieldProject.CustomFieldTypes.ViewActionButton, $SharePoint.Project.AssemblyFullName$</Field>
<RenderPattern Name="DisplayPattern">
<SetVar Name="ActionURLWithParams">
<HttpVDir CurrentWeb="TRUE" />
<HTML>/</HTML>
<Property Select="ActionURL"/>
<HTML>
<![CDATA[?ID=]]>
</HTML>
<Column Name="ID" />
<HTML>
<![CDATA[&List=]]>
</HTML>
<ListProperty Select="Name" />
</SetVar>
<Switch>
<Expr>
<Property Select="Format"/>
</Expr>
<Case Value="Button">
<HTML>
<![CDATA[<input type="button" class="]]>
</HTML>
<Property Select="CssClass"/>
<HTML>
<![CDATA[" value="]]>
</HTML>
<Property Select="DisplayName"/>
<HTML>
<![CDATA[" onclick="javascript:window.location=STSPageUrlValidation(']]>
</HTML>
<GetVar Name="ActionURLWithParams" />
<HTML>
<![CDATA[&Source=' + GetSource()); return false;" />]]>
</HTML>
</Case>
<Case Value="Hyperlink">
<HTML>
<![CDATA[<a class="]]>
</HTML>
<Property Select="CssClass"/>
<HTML>
<![CDATA[" href="]]>
</HTML>
<GetVar Name="ActionURLWithParams" />
<HTML>
<![CDATA[" onclick="javascript:this.href = unescapeProperly(escape(this.href)); GoToLink(this); return false;">]]>
</HTML>
<Property Select="DisplayName"/>
<HTML>
<![CDATA[<a />]]>
</HTML>
</Case>
<Default>
<HTML><![CDATA[<span style="font-style: italic;">Invalid format</span>]]></HTML>
</Default>
</Switch>
</RenderPattern>
<RenderPattern Name="EditPattern"></RenderPattern>
<RenderPattern Name="NewPattern" DisplayName="NewPattern"></RenderPattern>
<RenderPattern Name="PreviewDisplayPattern">
<HTML><![CDATA[<span style="font-style: italic;">View Action Button</span>]]></HTML>
</RenderPattern>
<RenderPattern Name="PreviewEditPattern"></RenderPattern>
<RenderPattern Name="PreviewNewPattern"></RenderPattern>
</FieldType>
</FieldTypes>
View Code   由于在SharePoint 2010中已经不推荐使用RenderPattern方式来呈现字段,需要将“CAMLRendering”设置成True,否则需要通过自定义XSLT来实现。



<Field Name="CAMLRendering">TRUE</Field>
  请根据实际情况修改下列内容:



<Field Name="FieldTypeClass">CustomFieldProject.CustomFieldTypes.ViewActionButton, $SharePoint.Project.AssemblyFullName$</Field>
  4. 创建自定义XSLT样式文件
  如果已经CAMLRendering设置成True,则不必自定义XSLT样式。
  目前仍然没有找到方法能够在XSLT中读取字段的自定义属性。





<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
version="1.0"
exclude-result-prefixes="xsl msxsl ddwrt"
xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:SharePoint="Microsoft.SharePoint.WebControls"
xmlns:ddwrt2="urn:frontpage:internal">
<xsl:template match ="FieldRef[@FieldType='ViewActionButton']" mode="Text_body">
<xsl:param name="thisNode" select="."/>
<xsl:variable name="ActionURL" select="current()/@ActionURL" />
<xsl:variable name="Format"  select="current()/@Format" />
<xsl:variable name="CssClass"  select="current()/@CssClass" />
<xsl:choose>
<xsl:when test="$Format='Button'">
<input type="button">
<xsl:attribute name="class">
<xsl:value-of select="$CssClass"/>
</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="$thisNode/@Name"/>
</xsl:attribute>
<xsl:attribute name="onclick">
javascript:window.location=STSPageUrlValidation('$ActionURL?ID=$thisNode/@ID&amp;List=$List');
</xsl:attribute>
</input>
</xsl:when>
<xsl:otherwise>
<a target="_blank">
<xsl:attribute name="class">
<xsl:value-of select="$CssClass"/>
</xsl:attribute>
<xsl:attribute name="href">
<xsl:value-of select="$ActionURL" />?ID=<xsl:value-of select="$thisNode/@ID" />&amp;List=<xsl:value-of select="$List" />
</xsl:attribute>
<xsl:value-of select="current()/@DisplayName"/>
</a>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
fldtypes_ViewActionButton.xsl  5. 下载
  ViewActionButton.zip

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-119440-1-1.html 上篇帖子: Configuring Multiple Authentication Providers for SharePoint 2007 下篇帖子: SharePoint PeopleEditor 控件的使用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表