前几天,用code-behind的方式做了个custom new form 。需要创建一个folder并将同时创建的item放到这个Folder里,在其他list里创建Folder(不同类型的list).发现了几个问题。现记录如下。
1.PeopleEditor 类的使用和加载
PeopleEditor这个类非常的有用。在custom list form的 时候难免会用到。但是他的加载是有限制的。在初始化赋值的时候,必须在其add到control里之前进行赋值并校验才能正常加载。这也就决定了其位置只能在createchildcontrol里或其之前执行的方法中,但是不能在onload里,因为onload只有在第一次加载的时候是在creatchildcontrol前,然后每次都会在其之后。
示例代码:
_Owner.CommaSeparatedAccounts = GetPepolEditDefaultValue(item["Owner"].ToString());
_Owner.Validate();
。。。
this.Controls.Add(_tableLayOut);//注意要在他之前
附:
private string GetPepolEditDefaultValue(string oValue)
{
string result = "";
System.Text.StringBuilder builder = new System.Text.StringBuilder();
string[] strs = oValue.Split('#');
if (strs == null)
return result;
for (int i = 1; i < strs.Length; i = i + 2)
{
if (strs[i].Substring(strs[i].Length - 1, 1).Equals(";"))
builder.Append(strs[i].Substring(0, strs[i].Length - 1));
else
builder.Append(strs[i]);
builder.Append(",");
}
result = builder.ToString();
if (!result.Equals(String.Empty))
result = result.Substring(0, result.Length - 1);
return result;
}
2.创建Folder同时创建Item并将其放到这个Folder中。
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = SPControl.GetContextSite(this.Context);
SPWeb web = site.OpenWeb();
using (SPSite elevSite = new SPSite(web.Site.ID))
{
string redirectUrl = string.Empty;
SPWeb elevWeb = elevSite.OpenWeb();
SPListItem mytmpItem = null;
if (!isModify)
{
try
{
SPList list = web.Lists["List"];//need to modify this parameter to your list name.
redirectUrl = list.RootFolder.ServerRelativeUrl;
mytmpItem = list.Folders.Add(redirectUrl, SPFileSystemObjectType.Folder);
mytmpItem["Title"] = "FolerName";
elevWeb.AllowUnsafeUpdates = true;
mytmpItem.Update();
elevWeb.AllowUnsafeUpdates = false;
SPListItemCollection folderColl = list.Folders;
for (int i = 0; i < folderColl.Count; i++)
{
if (folderColl[i].Folder.Name == this._tbContractName.Text)
{
SPFolder folder = folderColl[i].Folder;
SPListItemCollection itemColl = list.Items;
string folderpath = list.RootFolder.ServerRelativeUrl +"/"+ ItemName;//your Item name.
SPListItem item = null;
item = itemColl.Add(folderpath, SPFileSystemObjectType.File, null);
item = list.Items.Add();
setValues(web, item);
elevWeb.AllowUnsafeUpdates = true;
item.Update();
elevWeb.AllowUnsafeUpdates = false;
}
}
}
catch (Exception exe)
{
。。。
throw exe;
}
SPList listFolders = web.Lists["Folders"];//从一个list获取所有需要创建folder的List name.
SPListItem tmpItem = null;
foreach (SPListItem ititem in listFolders.Items)
{
try
{
SPList list = web.Lists[ititem["Title"].ToString()];
if (list != null)
{
if (list.BaseType == SPBaseType.DocumentLibrary)//注意不同的list类型创建Folder的方法是不同的。
{
SPFolder folder = list.RootFolder.SubFolders.Add(this._tbContractName.Text);
SPListItem item = folder.Item;
item["Title"] = this._tbContractName.Text;
item["ContentTypeId"] = "Folder";
elevWeb.AllowUnsafeUpdates = true;
item.Update();
elevWeb.AllowUnsafeUpdates = false;
}
else
{
tmpItem = list.Folders.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder);
tmpItem["Title"] = this._tbContractName.Text;
elevWeb.AllowUnsafeUpdates = true;
tmpItem.Update();
elevWeb.AllowUnsafeUpdates = false;
}
}
}
}
catch (Exception exc)
{
。。。
throw exc;
}
}
}
else
{
SPList list = web.Lists["List"];
SPListItemCollection folderColl = list.Folders;
for (int i = 0; i < folderColl.Count; i++)
{
if (folderColl[i].Folder.Name == ItemName)//need replace.
{
SPFolder folder = folderColl[i].Folder;
SPListItemCollection itemColl = list.Items;
redirectUrl = list.RootFolder.ServerRelativeUrl;
string folderpath = redirectUrl + "/" + ItemName;//replace this to your item name .
SPListItem item = null;
item = itemColl.GetItemById(Convert.ToInt32(Page.Request.QueryString["ID"].ToString()));
setValues(web, item);
elevWeb.AllowUnsafeUpdates = true;
item.Update();
elevWeb.AllowUnsafeUpdates = false;
}
}
}
this.Page.Response.Write("<script>window.location.replace('" + redirectUrl + "/AllItems.aspx');</script>");//redirect to your list all items page.
}
});
3.仿Sharepoint:FormField做成的控件。
这种控件校验成问题。直接的RequiredFieldValidator 对其校验不是很有作用。可以设置隐藏的字段,然后手动去触发他的校验时间去执行。
先这么多,后续发现会慢慢补充。