Frage

I'm using C# code to automatically create a read-only Note column that is a truncated version of an existing Note column.

list.Fields.Add(field.Title + " (truncated)", SPFieldType.Note, false);

At the moment, my new column has the title "OldColumnTitle (truncated)". This is what it shows up as when selecting columns for a View, which is good. Unfortunately, this is also what it shows up as in the view itself, which is bad.

If I leave off the + " (truncated)", there's no way in the Edit View form to tell the two apart.

The Title column does what I want - it has several different variations that show up in the Edit View form as "Title", "Title (linked to item)", "Title (linked to item with edit menu)" etc, but show up in the view itself as just "Title".

How can I replicate this? It's probably just a specific property on an SPField I've missed, but if so... I've missed it.

War es hilfreich?

Lösung

The string property you are looking for is SPField.AuthoringInfo. Unfortunately this is a readonly property. But the value of this property is stored in the SchemaXml property of the field. So you can set this value by altering the SchemaXml.

using (SPSite s = new SPSite("http://MyWeb"))
{
  using (SPWeb web = s.OpenWeb())
  {
    SPList l = web.Lists["MyList"];
    SPField f = l.Fields.GetField("MyColumn");

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(f.SchemaXml);
    XmlNode fieldNode = doc.FirstChild;

    XmlAttribute attr = fieldNode.Attributes["AuthoringInfo"];
    if (attr == null)
    {
      attr = doc.CreateAttribute("AuthoringInfo");
      fieldNode.Attributes.Append(attr);
    }
    attr.Value = "(truncated)";

    f.SchemaXml = doc.OuterXml;
    f.Update();
  }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit sharepoint.stackexchange
scroll top