Sharepoint设置和获取SPListItem字段值

单行文本、多行文本

1
2
3
4
5
//set
item["columnName"] = String;

//get
string fieldValue = item["columnName"].ToString();

选项(单选)

1
2
3
4
5
6
7
8
9
10
11
12
13
//set
item["columnName"] = list.Fields["columnName"].GetFieldValue("选项一");
//新增一个选项
var choiceField = list.Fields["columnName"] as SPFieldChoice;
if(!choiceField.Choices.Contains("选项二"))
{
    choiceField.AddChoice("选项二");
    choiceField.Update();
}
item["columnName"] = list.Fields["columnName"].GetFieldValue("选项二");

//get
string fieldValue = item["columnName"].ToString();

选项(多选)

1
2
3
4
5
6
7
8
9
10
11
12
13
//set
var choicevalues = new SPFieldMultiChoiceValue();
choicevalues.Add("选项一");
choicevalues.Add("选项二");
item["columnName"] = choicevalues;

//get
list.Fields["columnName"].ParseAndSetValue(item, choicevalues.ToString());
var multipleChoiceValues = new SPFieldMultiChoiceValue(item["columnName"].ToString());
for(int i = 0; i <= multipleChoiceValues.Count - 1; i++)
{
    //multipleChoiceValues[i];
}

数字、货币

1
2
3
4
5
6
7
8
9
10
11
//set
item["columnName"] = 12;
//or
item["columnName"] = Double.Parse("123.45");

//get
string fieldValue = item["columnName"].ToString();
//二位小数
Double.Parse(item["columnName"].ToString()).ToString("F2");
//格式化为货币
Double.Parse(item["columnName"].ToString()).ToString("C2");

百分数

1
2
3
4
5
6
7
8
9
//set
item["columnName"] = Double.Parse("0.8735");

//get
string fieldValue = item["columnName"].ToString();
//整数
Double.Parse(item["columnName"].ToString()).ToString("P0");
//二位小数
Double.Parse(item["columnName"].ToString()).ToString("P2");

日期和时间

1
2
3
4
5
6
7
8
9
10
11
//set
item["columnName"] = DateTime.Now;
//当前时间加一天
item["columnName"] = DateTime.Now.AddDays(1);

//get
var fieldValue = DateTime.Parse(item["columnName"].ToString());
//or
string fieldValue = item["columnName"].ToString();
//12/31/2012
DateTime.Parse(item["columnName"].ToString()).ToString("d");

查阅项(单选)

1
2
3
4
5
6
7
8
9
10
11
//set
var value = new SPFieldLookupValue(1, "A");
item["columnName"] = value;
//or
var lookupField = list.Fields["columnName"] as SPFieldLookup;
var lookupList = web.Lists[new Guid(lookupField.LookupList)];
var lookupitem = lookupList.Items[0];

//get
var lookupItemValue = new SPFieldLookupValue(item["columnName"].ToString());
fieldValue = lookupItemValue.LookupValue;//lookupItemValue.LookupId;

查阅项(多选)

1
2
3
4
5
6
7
8
9
10
11
12
13
//set
var spLookupValues = new SPFieldLookupValueCollection();
spLookupValues.Add(new SPFieldLookupValue(1, "A");
spLookupValues.Add(new SPFieldLookupValue(2, "B");
item["columnName"] = spLookupValues;

//get
SPFieldLookup fieldLookup = (SPFieldLookup)SupplierPricelist.Fields["columnName"];
SPFieldLookupValueCollection projects = (SPFieldLookupValueCollection)fieldLookup.GetFieldValue(QuotesItem["columnName"].ToString());
foreach (SPFieldLookupValue lookupValue in projects)
{
    //lookupValue.LookupValue;
}

是/否

1
2
3
4
5
//set
item["columnName"] = true;

//get
fieldValue = item["columnName"].ToString();

用户或用户组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//set
item["columnName"] = web.EnsureUser("canomo\\A");
//or
item["columnName"] = web.EnsureUser("[email protected]");
//or
SPWeb web = item.Web;
item["columnName"] = new SPFieldUserValue(web, userId.ToString());

//get
var fieldValue = new SPFieldUserValue(web, item["columnName"].ToString());
//fieldValue.User.Name
//or
public static SPFieldUserValue GetUserValue(SPListItem item, string columnName)
{
    SPFieldUser field = item.Fields[columnName] as SPFieldUser;
    if (field != null)
    {
        SPFieldUserValue fieldValue = field.GetFieldValue(item[columnName].ToString()) as SPFieldUserValue;
        return (fieldValue == null) ? null : fieldValue;
    }
    else
    {
        return null;
    }
}

用户或用户组(允许多重选择)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//set
var value = new SPFieldUserValueCollection(web, item["columnName"].ToString());
var personA = web.EnsureUser("canomo\\A");
var personAValue = new SPFieldUserValue(web, personA.ID, personA.LoginName);
value.Add(personAValue);
var personB = web.EnsureUser("canomo\\B");
var personBValue = new SPFieldUserValue(web, personB.ID, personB.LoginName);
value.Add(personBValue);
item["columnName"] = value;

//get
var fieldUserValueCollection = new SPFieldUserValueCollection(web, item["columnName"].ToString());
foreach(SPFieldUserValue fieldValue in fieldUserValueCollection)
{
    //fieldValue.User.Name
}

超链接或图片

1
2
3
4
5
6
//set
var value = new SPFieldUrlValue { Url = "http://www.canomo.com", Description = "canomo" };
item[columnName] = value;
//get
var fieldValue = new SPFieldUrlValue(item["columnName"].ToString());
//fieldValue.Description, fieldValue.Url

分享

文章导航