SetCustomProperty.Replace #sharepoint
using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
// SetCustomProperty GetCustomProperty在2013中无法使用,需要用下面的替换函数来执行
static void Main(string[] args)
{
SPSite site = new SPSite("http://svr0813:81");
SPWeb web = site.OpenWeb();
SPField field = web.Fields.GetField("测试");
SetFieldAttribute(field, "unitName", "吨");
field.Update();
var ss = GetFieldAttribute(field,"unitName");
Console.ReadKey();
}
static void SetFieldAttribute(object oo,string attribute, string value)
{
Type baseType;
BindingFlags flags;
MethodInfo mi;
baseType = typeof(SPField);
flags = BindingFlags.Instance | BindingFlags.NonPublic;
mi = baseType.GetMethod("SetFieldAttributeValue", flags);
mi.Invoke(oo, new object[] { attribute, value });
}
static string GetFieldAttribute(object oo ,string attribute)
{
Type baseType;
BindingFlags flags;
MethodInfo mi;
baseType = typeof(SPField);
flags = BindingFlags.Instance | BindingFlags.NonPublic;
mi = baseType.GetMethod("GetFieldAttributeValue",
flags,
null,
new Type[] { typeof(String) },
null);
object obj = mi.Invoke(oo, new object[] { attribute });
if (obj == null)
return "";
else
return obj.ToString();
}
}
}