ShartPoint v3中的列表Field属性的妙用

Sharepoint V3(以及Microsoft Office Sharepoint Server2007)真的给我们这些IT管理部门带来了很多的方便。尤其是对工作流的支持真的减轻了我们很多的工作。我们部门的ITIL的"事件管理"中事件登记和处理流程基本上不用开发代码,几乎用Sharepoint Designer就搞定了。但是,这里之所以用"几乎"两个字就是因为我还是写了一段代码来修改自定义列表中的Field的属性。

我在设计某个流程的时候,我很喜欢用自定义列表,因为这里的字段可以自己随意定义,另一个方面,是因为列表类型的列表它可以授权给用户只允许他看或修改自己创建的Item(不知道为什么没有在文档库中实现这个设置,这个设置其实很有用处)。尤其是v3中列表的增强更是如鱼得水。但是,也遇到这样的问题,某些字段,我们只希望它在创建的时候被修改、填写,在打开阅读的时候只读,但是在修改页面中却要让它无法修改。我一向懒得编程,因此就从Sharepoint的SDK中找了一个比较偷懒的办法——那就是通过修改Field的属性,让那些只让创建不让修改的字段从编辑视图中隐藏掉(详细的原理请参阅sharepoint的SDK)。在SPField 这个对象中有这样几个属性ShowInDisplayForm(在显示表单上显示),ShowInNewForm(在新建表单上显示),ShowInEditForm(在编辑表单上显示),我把需要在编辑表单上隐藏的那些字段设置成showineditForm=false,这样,当打开某个Item修改时,压根就看不见这个字段,所以也就是没有办法进行修改了。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using Microsoft.SharePoint;

 

namespace WSS列表自定义字段管理器

{

public partial class Form1 : Form

{

 

public SPSite siteCollection;

public SPWebCollection mySites;

public SPWeb mySite;

public SPListCollection myLists;

public SPList myList;

public string strMysiteName;

public string strMyListName;

public string strMyFieldName;

 

public Form1()

{

InitializeComponent();

}

 

private void Form1_Load(object sender, EventArgs e)

{

 

}

 

private void btnGetSites_Click(object sender, EventArgs e)

{

if (textBox1.Text == "")

{

MessageBox.Show("必须输入正确的URL地址:http://siteUrl");

}

else

{

string strURL = this.textBox1.Text; ;

try

{

siteCollection = new SPSite(strURL);

mySites = siteCollection.AllWebs;

cbSites.Enabled = true;

cbSites.Items.Clear();

foreach (SPWeb site in mySites)

{

 

cbSites.Items.Add(site.Name.ToString());

 

}

if (cbSites.Items.Count >= 1)

{

btnGetSiteLists.Enabled = true;

}

else

{

MessageBox.Show("没有找到相关站点,请检查地址的正确性!");

cbSites.Enabled = false;

}

}

catch (Exception ex1)

{

MessageBox.Show(ex1.Message);

 

}

// if siteCollection.

 

}

}

 

private void cbSites_SelectedIndexChanged(object sender, EventArgs e)

{

strMysiteName = cbSites.Text;

}

 

private void btnGetSiteLists_Click(object sender, EventArgs e)

{

try

{

mySite = siteCollection.AllWebs[strMysiteName];

myLists = mySite.Lists;

cbLists.Enabled = true;

cbLists.Items.Clear();

foreach (SPList list in myLists)

{

cbLists.Items.Add(list.Title.ToString());

 

}

btnGetFields.Enabled = true;

}

catch (Exception ex1)

{

MessageBox.Show(ex1.Message );

}

 

}

 

private void btnGetFields_Click(object sender, EventArgs e)

{

try

{

myList = mySite.Lists[strMyListName];

cbField.Enabled = true;

cbField.Items.Clear();

foreach(SPField field in myList.Fields)

{

cbField.Items.Add(field.Title.ToString() +":" +field.InternalName.ToString());

 

 

}

btnModefied.Enabled = true;

 

}

catch(Exception ex1)

{

MessageBox.Show(ex1.Message);

}

 

}

 

private void cbLists_SelectedIndexChanged(object sender, EventArgs e)

{

strMyListName = cbLists.Text;

 

}

 

private void btnModefied_Click(object sender, EventArgs e)

{

//判断是否隐藏

bool bIsHidden = false;

bool bIsRequid = false;

 

try

{

if (ckbHidden.Checked == true)

{

bIsHidden = true;

}

else

{

bIsHidden = false;

}

 

 

 

//判断是否必须填写

if (ckbRequid.Checked == true)

{

bIsRequid = true;

}

else

{

bIsRequid = false;

}

//现在的字段

SPFieldCollection myFields = myList.Fields;

SPField myField = new SPField(myFields, strMyFieldName);

myField.Hidden = bIsHidden;

myField.Required = bIsRequid;

myField.ShowInDisplayForm = ckbShowInDisplayForm.Checked;

myField.ShowInNewForm = ckbShowInNewForm.Checked;

myField.ShowInEditForm = ckbShowInEditForm.Checked;

myField.ReadOnlyField = ckbReadOnly.Checked;

 

 

myField.Update();

MessageBox.Show("更新完成!");

}

catch(Exception ex1)

{

MessageBox.Show(ex1.Message);

}

}

 

private void cbField_SelectedIndexChanged(object sender, EventArgs e)

{

strMyFieldName = cbField.Text;

int nLen = strMyFieldName.Length;

int nPos = strMyFieldName.IndexOf(":")+1;

if (nPos > 0)

{

strMyFieldName = strMyFieldName.Substring(nPos , nLen - nPos);

}

 

 

}

 

 

 

private void btnDispField_Click(object sender, EventArgs e)

{

//现在的字段

SPFieldCollection myFields = myList.Fields;

SPField myField = new SPField(myFields, strMyFieldName);

string str1 = "";

str1 = myField.TypeDisplayName.ToString();

str1 = str1 +":" +myField.TypeAsString.ToString();

// str1 = str1 + ":" + myField.FieldValueType.ToString();

MessageBox.Show(str1);

}

 

 

}

}

你可能感兴趣的:(Field)