1、显示控件有:“列表框”和“组合框”两种
2、组合框:第一个字段为“填充字段”
3、通过查阅向导建立的会自动添加“关系”(外键),因此在删除或修改字段时需要先删除二者关系,最好不通过“查阅向导”建立。
1、显示控件有:“列表框”和“组合框”两种
2、组合框:第一个字段为“填充字段”
3、通过查阅向导建立的会自动添加“关系”(外键),因此在删除或修改字段时需要先删除二者关系,最好不通过“查阅向导”建立。
目录
在数据库中向表中的某个字段输入数据时,经常出现输入的数据是一个数据集合中的某个值的情况。例如教师职称_定是“教授、副教授、讲师、助教竹这个数据集合中的其中一个元素的值。对于输入这种数据的字段列,最简单的方法是把该字段列设置为“查阅向导”数据类型。严格地说“查阅向导”不是一种新的数据类型,它是建立一种在某个数据集合中选择数据值的关系。Access提供的这种数据类型给用户带来了很大的方便。
当完成字段的查阅设置后,在这个字段输人数据时就可以不用输入数据,而从一个列表中选择数据,这样既加快了数据输入的速度又保证了输人数据的正确性。
查阅字段数值的来源有两种:来自表、查询中的数值和来自创建值列表的数值,
其实就是一句查询语句
这个查询需要建立一个表关系
多值字段是Access中的一种新的字段。一个字段具有多个值,在客观世界中常常出现。
例如:一个超市的某一种商品由多个供应商供货。使用多值字段功能,可以创建一个从列表中选择多个供应商的多值字段。选定的供应商存储在多值字段中,显示时由逗号一(默认情况下)分隔。Access提供的多值字段功能,使得用户处理这类原来比较复杂的问题变得非常容易了。
令人感到不解的是为什么Access允许在一个字段中存储多个值,按照关系数据库的理论是禁止这样做的。其实,在Access中表面是把多个值存储在一个字段中,用户看到和使用的似乎是一个字段,但这些值实际上单独存储。Access非常巧妙地把它们存储在一个隐藏的系统表中进行管理,用户完全觉察不到.Access数据库引擎帮用户处理这些复杂工作,自动分隔数据并将它们重新收集在一起,就像它们是在一个字段中一样。
在罗斯文数据库中创建一个产品供应商表,在表中插入一个供应商的多值字段列。(建议先建一个罗斯文数据库副本,在副本中练习)
创建一个表,添加一个“产品字段”
1、首先,打开VS创建两个List Definition,分别是Address和City,如下图:
2、City列表里修改Title为City Name,其实内部名称还是Title,注意一下:
3、给City的列表实例,添加几个值,用来测试使用,如下:
4、在Address列表里添加几个字段,分别是CityName(LookUp类型)和HomeAddress(Single Line of Text),如下:
5、查看Address列表的Schema.Xml,尤其是Fields节点,也就是字段,如下图:
6、修改LookUp字段类型的Xml,主要是CityName的List属性和ShowField属性,分别是关联的列表的相对地址和显示字段,如下:
<Fields> <Field Name="Title" ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" DisplayName="$Resources:core,Title;" Type="Text" Required="TRUE" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="Title" MaxLength="255" /> <Field Name="CityName" ID="{5aea19dc-5b16-41e1-ad59-ca6ad2e585ed}" DisplayName="CityName" Type="Lookup" List="Lists/City" ShowField="Title"/> <Field Name="HomeAddress" ID="{caf96d2a-6e41-4ea8-8dfc-13fe1c3a2d09}" DisplayName="HomeAddress" Type="Text" /> </Fields>
7、在Address列表的列表实例中插入测试数据,注意查阅项的格式,ID;#Field Value,如下图:
8、部署列表,可以在网站内容中看到Address和City两个列表,如下图:
9、查看Address,我们的查阅项字段(CityName)显示正常,如下图:
10、在列表设置中,查看栏的属性,可以看到关联的列表是City,关联的列是City Name,注意关联的时候要用内部字段,显示的时候会是字段名称,如下图:
11、编辑项目,可以看到查阅项字段,如下:
总结
一直创建查阅项字段,List的值设置的都是Guid,然后代码创建的时候,定义的列表没有Guid,所以很纠结创建查阅项字段;偶然的一个想法,用相对地址关联,尝试一下也可以,所以拿出来分享给大家。
当然,自己也试过,给列表定义New一个Guid,然后用它关联,结果发现并没有成功,或许是因为部署顺序的问题或者其他原因,自己也未深究。好了就到这里,希望本文能对需要的人有所帮助。
本文主要是整理记录下,如何通过SharePoint在服务端对象模型中如何对列表各种字段类型赋值和取值操作。
这是得到SPListItem的基本代码。如果列表中包含至少一条数据,检索的第一项,否则创建新项目。
var web = SPContext.Current.Site.RootWeb;
var list = web.Lists.TryGetList("fieldslist");
if (list == null) return;
SPListItem item;
item = list.ItemCount > 0 ? list.Items[0] : list.Items.Add();
var t = DateTime.Now.ToLongTimeString();
//StringBuilder for output
var s = new StringBuilder();
//Variable for storing temporary values
String value;
item["Title"] = String.Format("Title updated at {0}",t);
value = item["Title"].ToString();
s.AppendLine(String.Format("Title Field: {0}<br></br>", value));
item["textfield"] = String.Format("At {0} dogs still can't write poems", t);
item.Update();
value = item["textfield"].ToString();
s.AppendLine(String.Format("Text Field: {0}<br></br>", value));
item["notefield"] = String.Format("At {0} dogs still can't write poems. \r\nBut then, neither can I!", t);
item.Update();
value = item["notefield"].ToString();
s.AppendLine(String.Format("Note Field: {0}<br></br>", value));
item["yesnofield"] = false;
item.Update();
value = item["yesnofield"].ToString();
s.AppendLine(String.Format("Yes/No Field: {0}<br></br>", value));
item["numberfield"] = 35;
//Or
item["numberfield"] = Double.Parse("354.67");
item.Update();
value = item["numberfield"].ToString();
s.AppendLine(String.Format("Number Field: {0}<br></br>", value));
value = Double.Parse(item["numberfield"].ToString()).ToString("F1");
s.AppendLine(String.Format("Number Field (one decimal place): {0}<br></br>", value));
value = Double.Parse(item["numberfield"].ToString()).ToString("F0");
s.AppendLine(String.Format("Number Field (two decimal places): {0}<br></br>", value));
货币字段使用字段类型相同的 SharePoint 作为数 (SPFieldNumber)。数字字段的类型是double。您可以使用标准数字格式指定要设置格式的数字显示,具体地说,格式化为一种货币。请参阅Double.ToString的详细信息。
item["currencyfield"] = Double.Parse("354.67");
item.Update();
value = item["currencyfield"].ToString();
s.AppendLine(String.Format("Currency Field: {0}<br></br>", value));
value = Double.Parse(item["currencyfield"].ToString()).ToString("C2");
s.AppendLine(String.Format("Currency Field (formatted as a currency): {0}<br></br>", value));
value = (Double.Parse(item["numberfield"].ToString()) + 123).ToString("C2");
s.AppendLine(String.Format("Currency Field (addition): {0}<br></br>", value));
百分比字段使用字段类型相同的 SharePoint 作为数 (SPFieldNumber)。数字字段的类型是double。您可以使用标准数字格式指定要设置格式的数字显示,具体地说,格式为百分比。请参阅Double.ToString的详细信息。
item["percentfield"] = Double.Parse("0.8735");
item.Update();
value = item["percentfield"].ToString();
s.AppendLine(String.Format("Percent Field: {0}<br></br>", value));
value = Double.Parse(item["percentfield"].ToString()).ToString("P0");
s.AppendLine(String.Format("Percent Field (formatted as a percent, and as a whole number): {0}<br></br>", value));
value = Double.Parse(item["percentfield"].ToString()).ToString("P2");
s.AppendLine(String.Format("Percent Field (formatted as a percent, with two decimal places): {0}<br></br>", value));
若要设置一个日期字段,使用 System.DateTime 对象创建一个日期,然后将 DateTime 对象分配给列表项的字段。当您检索日期时间字段的值时,可以使用标准日期格式说明符来设置格式的值的输出。请参阅DateTime.ToString的详细信息。
item["datefield"] = DateTime.Now;
//Or, set the date to Now + two days
item["datefield"] = DateTime.Now.AddDays(2);
item.Update();
取值
value = item["datefield"].ToString();
s.AppendLine(String.Format("Date Field: {0}<br></br>", value));
value = DateTime.Parse(item["datefield"].ToString()).ToString("d");
s.AppendLine(String.Format("Date Field (using the \"6/15/2008\" format): {0}<br></br>", value));
value = DateTime.Parse(item["datefield"].ToString()).ToString("D");
s.AppendLine(String.Format("Date Field (using the \"Sunday, June 15, 2008\" format): {0}<br></br>", value));
value = DateTime.Parse(item["datefield"].ToString()).ToString("R");
s.AppendLine(String.Format("Date Field (using the \"Sun, 15 Jun 2008 21:15:07 GMT\" format): {0}<br></br>", value));
var dateValue = DateTime.Parse(item["datefield"].ToString());
value = dateValue.AddDays(13).ToString("dd-MMM-yy");
s.AppendLine(String.Format("Date Field (using a custom display format): {0}<br></br>", value));
单选赋值
var choicevalues = new SPFieldMultiChoiceValue();
choicevalues.Add("Green");
choicevalues.Add("Blue");
item["multiplechoicefield"] = choicevalues;
item.Update();
单选取值
value = item["choicefield"].ToString();
s.AppendLine(String.Format("Choice Field: {0}<br></br>", value));
多选取值
list.Fields["multiplechoicefield"].ParseAndSetValue(item, choicevalues.ToString());
var multipleChoiceValues = new SPFieldMultiChoiceValue(item["multiplechoicefield"].ToString());
s.AppendLine(String.Format("Multiple Choice Field: {0}<br></br>", multipleChoiceValues));
for(int i = 0; i Multiple Choice Field, value {0}: {1}<br></br>", i, multipleChoiceValues[i]));
}
多选赋值
var choicevalues = new SPFieldMultiChoiceValue();
choicevalues.Add("Green");
choicevalues.Add("Blue");
item["multiplechoicefield"] = choicevalues;
item.Update();
单用户取值
value = item["personfield"].ToString();
s.AppendLine(String.Format("Person Field: {0}<br></br>", value));
var userFieldValue = new SPFieldUserValue(web, item["personfield"].ToString());
s.AppendLine(String.Format("Person Field: Name = {0}, Email = {1}<br></br>", userFieldValue.User.Name, userFieldValue.User.Email));
单用户赋值
item["personfield"] = web.EnsureUser("contoso\\fred");
//or
item["personfield"] = web.EnsureUser("fred@contoso.com");
item.Update();
多用户取值
var fieldUserValueCollection = new SPFieldUserValueCollection(web, item["lotsofpeoplefield"].ToString());
s.AppendLine(String.Format("MultiPerson Field: {0}<br></br>", fieldUserValueCollection));
var userCount = 1;
foreach(SPFieldUserValue v in fieldUserValueCollection)
{
s.AppendLine(String.Format("MultiPerson Field, value {0}: {1}<br></br>", userCount, v.User.Name));
userCount++;
}
多用户赋值
var lotsofpeople = new SPFieldUserValueCollection(web, item["lotsofpeoplefield"].ToString());
var personA = web.EnsureUser("contoso\\fred");
var personAValue = new SPFieldUserValue(web, personA.ID, personA.LoginName);
var personB = web.EnsureUser("contoso\\barnie");
var personBValue = new SPFieldUserValue(web, personB.ID, personB.LoginName);
lotsofpeople.Add(personAValue);
lotsofpeople.Add(personBValue);
item["lotsofpeoplefield"] = lotsofpeople;
item.Update();
赋值
var lookupField = list.Fields["lookupfield"] as SPFieldLookup;
var lookupList = web.Lists[new Guid(lookupField.LookupList)];
var lookupitem = lookupList.Items[0];
//-or-
//lookupitem = lookupList.GetItemByUniqueId(new Guid("fc71b84c-74d4-4f7c-9eed-fb7a5fbe24a6"));
//-or-
//lookupitem = lookupList.GetItemById(1);
var lookupValue = new SPFieldLookupValue(lookupitem.ID, lookupitem.ID.ToString());
item["lookupfield"] = lookupValue;
item.Update();
取值
var lookupItemValue = new SPFieldLookupValue(item["lookupfield"].ToString());
value = lookupItemValue.LookupValue;
s.AppendLine(String.Format("Lookup Field: {0}<br></br>", value));
赋值
var hyperlinkField = list.Fields["hyperlinkfield"] as SPFieldUrl;
var urlFieldValue = new SPFieldUrlValue();
urlFieldValue.Description = "Microsoft";
urlFieldValue.Url = "http://www.microsoft.com ";
//SharePoint 2013 Only
hyperlinkField.ValidateParseAndSetValue(item, urlFieldValue.ToString());
//SharePoint 2010 and SharePoint 2013
hyperlinkField.ParseAndSetValue(item, urlFieldValue.ToString());
item.Update();
取值
var hyperlinkFieldValue = new SPFieldUrlValue(item["hyperlinkfield"].ToString());
value = String.Format("<a href="\"{1}\"">{0}</a>", hyperlinkFieldValue.Description, hyperlinkFieldValue.Url);
s.AppendLine(String.Format("Hyperlink Field: {0}<br></br>", value));
赋值
var managedMetaDataField = list.Fields["managedmetadatafield"] as TaxonomyField;
var termsetId = managedMetaDataField.TermSetId;
var termstoreId = managedMetaDataField.SspId;
var taxonomySession = new TaxonomySession(web.Site);
var termstore = taxonomySession.TermStores[termstoreId];
var termset = termstore.GetTermSet(termsetId);
var termname = "Rubbish Tip";
var terms = termset.GetTerms(termname, false);
Term term;
if(terms.Count == 0)
{
term = termset.CreateTerm(termname, termstore.Languages[0]);
termstore.CommitAll();
}
else
{
term = terms[0];
}
managedMetaDataField.SetFieldValue(item, term);
item.Update();
取值
var taxonomyFieldValue = item["managedmetadatafield"] as TaxonomyFieldValue;
s.AppendLine(String.Format("Taxonomy Field: {0} ({1})<br></br>", taxonomyFieldValue.Label, taxonomyFieldValue.TermGuid));
多值的托管元数据字段赋值
var managedMetaDataField = list.Fields["managedmetadatafield"] as TaxonomyField;
var termsetId = managedMetaDataField.TermSetId;
var termstoreId = managedMetaDataField.SspId;
var taxonomySession = new TaxonomySession(web.Site);
var termstore = taxonomySession.TermStores[termstoreId];
var termset = termstore.GetTermSet(termsetId);
var multipleManagedMetaDataField = list.Fields["multiplemanagedmetadatafield"] as TaxonomyField;
var termCollection = new TaxonomyFieldValueCollection(multipleManagedMetaDataField);
var taxonomyLabels = new[] {"Frog Catcher", , "Giraffe Stealer" "Moon Dog"};
Term term;
foreach(var label in taxonomyLabels)
{
var terms = termset.GetTerms(label, false);
term = null;
if(terms.Count == 0)
{
term = termset.CreateTerm(label, termstore.Languages[0]);
termstore.CommitAll();
}
else
{
term = terms[0];
}
var termValue = new TaxonomyFieldValue(multipleManagedMetaDataField);
termValue.TermGuid = term.Id.ToString();
termValue.Label = term.Name;
termCollection.Add(termValue);
}
multipleManagedMetaDataField.SetFieldValue(item, termCollection);
item.Update();
多值的托管元数据字段取值
var taxonomyFieldValueCollection = item["multiplemanagedmetadatafield"] as TaxonomyFieldValueCollection;
value = String.Empty;
foreach(var taxonomyValue in taxonomyFieldValueCollection)
{
value = String.IsNullOrEmpty(value)
? String.Format("{0} ({1})", taxonomyValue.Label, taxonomyValue.TermGuid)
: String.Format("{0}, {1} ({2})", value, taxonomyValue.Label, taxonomyValue.TermGuid);
//Or, to use get the term
var currentTerm = termstore.GetTerm(new Guid(taxonomyValue.TermGuid));
//do something with the term
}
s.AppendLine(String.Format("Multiple Taxonomy Field Values: {0})<br></br>", value));
计算字段的值是在创建或更新列表项时计算的。它是不可能直接设置此值。可以使用以下方法来设置公式,用以计算字段值。在这些方法之前,有四个主要的属性,可以设置在计算的字段 ;公式、 输出类型、 DisplayFormat 和简写。您需要设置,哪些属性取决于计算值。
公式: 用来计算值的公式。
输出类型: 从计算结果值的类型。受支持的类型,文本、 数字、 整数、 货币、 boolean 类型的值和日期时间。
DisplayFormat: 提供号码、 整数和货币用于指定的小数位数
简写: 与日期时间用于指定日期或日期和时间。
在以下示例中,我们执行以下任务:
//Configuring the calculated field
s.AppendLine(String.Format("Calculated Field Formula: {0}<br></br>", calculatedfield.Formula));
s.AppendLine(String.Format("Calculated Display Format: {0}<br></br>", calculatedfield.DisplayFormat));
s.AppendLine(String.Format("Calculated Output Type: {0}<br></br>", calculatedfield.OutputType));
calculatedfield.DisplayFormat = SPNumberFormatTypes.FourDecimals;
calculatedfield.Update();
item.Update();
s.AppendLine(String.Format("Calculated Field (Display Format 4 decimals): {0}<br></br>",
calculatedfield.GetFieldValueAsText(item["calculatedfield"])));
calculatedfield.OutputType = SPFieldType.Integer;
calculatedfield.Update();
item.Update();
s.AppendLine(String.Format("Calculated Field (Output Type Integer): {0}<br></br>", calculatedfield.GetFieldValueAsText(item["calculatedfield"])));
calculatedfield.Formula = "=[datefield]+90";
calculatedfield.DateFormat = SPDateTimeFieldFormatType.DateOnly;
calculatedfield.OutputType = SPFieldType.DateTime;
calculatedfield.Update();
item.Update();
s.AppendLine(String.Format("Calculated Field (Updated Formula, Date Format and Output Type):{0}<br></br>", calculatedfield.GetFieldValueAsText(item["calculatedfield"])));
计算的字段的工作方式不同于正常的领域。在列表字段,建立计算公式和时添加或更新列表项,为该列表项的列的值根据计算公式。
获取一个值,计算字段
获取计算的字段的引用。然后获取列表项的引用。最后,调用 GetFieldValueAsText
方法,通过在项目对象计算字段的值。
var calculatedfield = list.Fields["calculatedfield"] as SPFieldCalculated;
value = calculatedfield.GetFieldValueAsText(item["calculatedfield"]);
s.AppendLine(String.Format("Calculated Field: {0}<br></br>", value));