Lumot: Halimbawa functional - Custom Data Uri

Negosyo Sitwasyon:

Enterprise-wide pagpapatupad ng Moss para sa pagmamanupaktura kumpanya na may 30+ mga site at ilang dosena corporate mga kagawaran.

Layunin ng Negosyo:

Sa kabila ng maraming mga grupo ng negosyo (kagawaran, lokasyon, at iba pa), tiyak na data ay dapat na pinananatili sa isang pandaigdigang antas. Halimbawa, isang makapangyarihan master list ng lahat ng pisikal na lokasyon ng mga kumpanya (e.g. manufacturing facility, warehouse locations, sales offices) should be maintained in a central location.

Technical Problem:

The enterprise taxonomy was implemented using multiple site collections. We would have liked to create the authoritative list of physical locations in a custom WSS list. Pagkatapos, when we needed to have a column in a content type (or a column added to a list or doc library) that contained corporate locations, we would create a column using the "lookup" datatype and point to this master list.

Unfortunately, lookup datatypes must access a source list "locally" meaning that our authoritative list cannot span site collections.

Teknikal na Solusyon:

Implement a new custom data type implemented based on SPField and represented as a DropDownList in the UI whose ListItems populate from the master WSS list.

We created a new site collection called "http://localhost/EnterpriseData". There, we created a custom list named "Corporate Locations". This list just uses the standard "Title" field to contain the list of actual corporate locations.

One follows several discrete steps to create a custom data type in WSS. They are:

  1. Define a class which inherits from SPField (one may inherit from other fields if required).

Here is the code for that:

publiko klase XYZZYCorporateLocationField : SPFieldText
{
publiko XYZZYCorporateLocationField
(SPFieldCollection patlang, pisi typeName, pisi displayName)
: base(patlang, typeName, displayName) { }

publiko XYZZYCorporateLocationField
(SPFieldCollection patlang, pisi displayName)
: base(patlang, displayName) { }

publiko magpawalang-bisa BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl control = bago XYZZYCorporateLocationFieldControl();
control.FieldName = ito.InternalName;
bumalik control;
} //get
} // fieldrenderingcontrol

publiko magpawalang-bisa pisi GetValidatedString(bagay halaga)
{
kung (ito.Required || value.ToString().Kapantay(String.Empty))
{
magtapon bago SPFieldValidationException ("Department is not assigned.");
}
bumalik base.GetValidatedString(halaga);
} // getvalidatedstring

} // XYZZYCorporateLocation

  1. Define another class that inherits from the base field control, tulad ng sa:

publiko klase XYZZYCorporateLocationFieldControl : BaseFieldControl
{
protektado DropDownList XYZZYCorporateLocationSelector;

protektado magpawalang-bisa pisi DefaultTemplateName
{
get
{
bumalik "XYZZYCorporateLocationFieldControl";
}
} // DefaultTemplateName

publiko magpawalang-bisa bagay Halaga
{
get
{
EnsureChildControls();
bumalik ito.XYZZYCorporateLocationSelector.SelectedValue;
} // get
nakatakda
{
EnsureChildControls();
ito.XYZZYCorporateLocationSelector.SelectedValue = (pisi)ito.ItemFieldValue;
} // nakatakda
} // override object Value

protektado magpawalang-bisa walang bisa CreateChildControls()
{

kung (ito.Field == sero || ito.ControlMode == SPControlMode.Display)
bumalik;

base.CreateChildControls();

ito.XYZZYCorporateLocationSelector =
(DropDownList)TemplateContainer.FindControl("XYZZYCorporateLocationSelector");

kung (ito.XYZZYCorporateLocationSelector == sero)
magtapon bago Kataliwasan("ERROR: Cannot load .ASCX file!");

kung (!ito.Page.IsPostBack)
{

paggamit (SPSite site = bago SPSite("http://localhost/enterprisedata"))
{
paggamit (SPWeb web = site.OpenWeb())
{

SPList currentList = web.Lists["Corporate Locations"];

foreach (Magtuhog XYZZYCorporateLocation sa currentList.Items)
{
kung (XYZZYCorporateLocation["Title"] == sero) magpatuloy;

pisi theTitle;
theTitle = XYZZYCorporateLocation["Title"].ToString();

ito.XYZZYCorporateLocationSelector.Items.Add
(bago ListItem(theTitle, theTitle));

} // foreach

} // using spweb web = site.openweb()
} // using spsite site = new spsite("http://localhost/enterprisedata")

} // if not a postback

} // CreateChildControls

} // XYZZYCorporateLocationFieldControl

The above code basically implements the logic for populating the DropDownList with values from the WSS custom list located at http://localhost/enterprisedata and named "Corporate Departments".

I defined both classes in a single .cs file, compiled it and put it into the GAC (strong required, mangyari pa).

  1. Implement a control template (.ascx) tulad ng ipinapakita:

<%@ Control Language="C#" Inherits="Microsoft.SharePoint.Portal.ServerAdmin.CreateSiteCollectionPanel1,Microsoft.SharePoint.Portal,Version=12.0.0.0,Culture=neutral,PublicKeyToken = 71e9bce111e9429c" compilationMode="Always" %>
<%
@ Magparehistro Tagprefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Bersyon = 12.0.0.0, Culture = neutral, PublicKeyToken = 71e9bce111e9429c" %> <%@ Magparehistro Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Bersyon = 12.0.0.0, Culture = neutral, PublicKeyToken = 71e9bce111e9429c" %>
<SharePoint:RenderingTemplate ID="XYZZYCorporateLocationFieldControl" runat="server">
<Template>
<asp:DropDownList ID="XYZZYCorporateLocationSelector" runat="server" />
</Template>
</
SharePoint:RenderingTemplate>

The above is saved into c:\program files\common files\microsoft shared\web server extensions\12\controltemplates.

  1. Sa wakas, we create an XML file to save into the …..\12\XML directory. This is CAML that defines our custom data type and for my example, looks like this:

<?xml salin="1.0" pag-encode="utf-8" ?>
<
FieldTypes>
<
FieldType>
<
Field Pangalan="TypeName">CorporateLocations</Field>
<
Field Pangalan="ParentType">Teksto</Field>
<
Field Pangalan="TypeDisplayName">Corporate Locations</Field>
<
Field Pangalan="TypeShortDescription">All XYZZY Corporate locations including manufacturing or other facilities.</Field>
<
Field Pangalan="UserCreatable">TRUE</Field>
<
Field Pangalan="ShowInListCreate">TRUE</Field>
<
Field Pangalan="ShowInDocumentLibraryCreate">TRUE</Field>
<
Field Pangalan="ShowInSurveyCreate">TRUE</Field>
<
Field Pangalan="ShowInColumnTemplateCreate">TRUE</Field>
<
Field Pangalan="FieldTypeClass">Conchango.XYZZYCorporateLocationField, XYZZYCorporateLocationField, Bersyon = 1.0.0.0, Culture = neutral, PublicKeyToken=b0b19e85410990c4</Field>
<
RenderPattern Pangalan="DisplayPattern">
<
Switch>
<
Expr>
<
Column />
</
Expr>

<Case Halaga=""/>

<Default>
<
HTML>
<![CDATA[
<span style="color:Red"><b>]]>
</
HTML>

<
Column SubColumnNumber="0" HTMLEncode="TRUE"/>

<HTML><![CDATA[</b></maikling panahon>]]></HTML>

</
Default>
</
Switch>

</
RenderPattern>
</
FieldType>
</
FieldTypes>
This XML file adds the custom data type to the WSS "library" and matches it up against the GAC’d assembly.

After moving all these bits into place, iisreset on the server and it should all start working nicely.

3 ano sa tingin mo "Lumot: Halimbawa functional - Custom Data Uri

  1. Alejandro
    Hi Paul,
    First of all, thanks for the article, because it’s very interesting. Only one question;
    Do you know if it’s posible to render correctly a custom field type in the datasheet view of a list?
    Because every custom field type I create it’s shown as read-only in datasheet view (and the MSDN, halimbawa, doesn’t help me very much :-S).
    Salamat
    Sumagot
  2. Lyndsay

    I am attempting to implement your solution. However I instead of DropDownList, I only have the option for a DropDownChoiceList. Do you happen to know how to add items to a DropDownChoiceList? We are using SharePoint 2007 SP1 and Visual Studio 2005 SP1.

    Sumagot

-Iwan ng sagot

Ang iyong email address ay hindi nai-publish. Mga kinakailangang patlang ay minarkahan *