是否可以在SharePoint中创建自定义上载页面,该页面将文件上载控件与自定义字段类型相结合,以便用户可以选择要从其硬盘驱动器上载的文件,输入文件的标题,还可以添加注释,指定内容类型并将其他数据输入到多个自定义字段中并创建新的SPListItem,将文件上载并关联到新的SPListItem,最后将输入到自定义字段中的所有值成功保存到新创建的SPListItem中?
注意:我希望使用SharePoint自定义字段类型执行此任务仅,而 NOT 只需使用自定义ASPX页面UserControls。
使用自定义字段类型时存在的基本问题是SharePoint文档库中的文件上载事件是异步事件。您可以覆盖SPListItemEventReceiver中可用的ItemAdding方法的默认行为,当文件 “处于上载状态时 时,该方法可用于访问某些信息并且您可以同样从ItemAdded方法中访问有关新创建的SPListItem的信息,该方法在已添加项目之后被称为 “ - 但由于此方法单独发生线程并执行 ASYNCHRONOUSLY ,不知道与自定义字段或其各自值相关的任何内容,用户在这些字段中输入的数据都没有成功保存。
当用户希望通过使用EditFormTemplate编辑自定义字段中的值来 更新 有关文档的信息时,将在初始化期间设置每个字段的SPListItem属性。这一切都很好,因为在这种情况下ListItem已经存在!问题是,当用户希望第一次上传文档时,ListItem显然不存在,所以每个字段都初始化,SPListItem属性设置为“null”,并且将永远保持为null,因为它只是没有似乎是通过引用新创建的ListItem AFTER 来追溯更新每个字段的ListItem属性的任何方法!
正是出于这个原因,仅此原因导致微软坚持强迫用户在一个屏幕上上传文件,然后在文件成功上传后将其重定向到编辑表单。通过拆分这两个页面,Microsoft强制用户上传文件并创建ListItem PRIOR 以保存有关该文件的任何其他信息。上传文件并创建ListItem后,将每个自定义字段的值保存回ListItem 没有问题,因为ListItem已经存在 !
注意: BaseFieldControl继承自FieldMetadata,它继承自FormComponent。 FormComponent有一个名为 Item 的属性,它对应于该字段所属的底层SPListItem。 BaseFieldControl有一个名为 ListItemFieldValue 的属性,它存储保存回ListItem的字段的实际值,它还有一个可以使用的可覆盖的方法,名为 UpdateFieldValueInItem()在将数据分配给 ItemFieldValue 属性之前执行其他逻辑(例如验证)。
当 更新 EXISTING SPListItem时,以下代码有效且自定义字段值将被保存因为SPListItem已经存在!
var item = MyDocLib.Items[0] as SPListItem;
item["MyCustomFieldName"] = "some value";
item.Update();
在SPListItemEventReceiver中,在初始文件上载期间,在创建ListItem并且单个自定义字段值“尝试保存”之后,ItemUpdating / ItemUpdated方法将包含SPItemEventProperties属性的空引用.ListItem,因为,如上所述以前,ItemAdded方法是异步触发的,新创建的ListItem在ItemUpdating / ItemUpdated方法中不可用。
答案 0 :(得分:0)
要上传文件并将其与列表项链接,您可以使用Sparqube Document字段类型。 注意:这是商业插件。
答案 1 :(得分:0)
好的,所以创建一个自定义上传表单,它将文件上传输入控件和库中的自定义字段与一个或多个OOTB或自定义 SPListFieldIterators 相结合,这不是一件容易的事 - 这可能是微软的原因决定将这个过程分成两个截然不同且完全不相关的行动。
然而,允许这种功能有一个固有的价值,因为它使用户能够同时上传文件并在单个原子操作中保存元数据,这样你的库中永远不会存在一个文件。没有任何识别信息的“以太”。
那需要什么?好几件事。
第一个是创建一个实用程序类,我称之为“ FileUploader ”,这就是它的样子。
public class FileUploader
{
#region Fields
private readonly SPList list;
private readonly FileUpload fileUpload;
private string contentTypeId;
private string folder;
private SPContext itemContext;
private int itemId;
#endregion
#region Properties
public bool IsUploaded
{
get
{
return this.itemId > 0;
}
}
public SPContext ItemContext
{
get
{
return this.itemContext;
}
}
public int ItemId
{
get
{
return this.itemId;
}
}
public string Folder
{
get
{
return this.folder;
}
set
{
this.folder = value;
}
}
public string ContentTypeId
{
get
{
return this.contentTypeId;
}
set
{
this.contentTypeId = value;
}
}
#endregion
public FileUploader(SPList list, FileUpload fileUpload, string contentTypeId)
{
this.list = list;
this.fileUpload = fileUpload;
this.contentTypeId = contentTypeId;
}
public FileUploader(SPList list, FileUpload fileUpload, string contentTypeId, string folder)
{
this.list = list;
this.fileUpload = fileUpload;
this.contentTypeId = contentTypeId;
this.folder = folder;
}
public event EventHandler FileUploading;
public event EventHandler FileUploaded;
public event EventHandler ItemSaving;
public event EventHandler ItemSaved;
public void ResetItemContext()
{
//This part here is VERY, VERY important!!!
//This is where you "trick/hack" the SPContext by setting it's mode to "edit" instead
//of "new" which gives you the ability to essentially initialize the
//SPContext.Current.ListItem and set it's ItemId value. This of course could not have
//been accomplished before because in "new" mode there is no ListItem.
//Once you've done all that then you can set the FileUpload.itemContext
//equal to the SPContext.Current.ItemContext.
if (this.IsUploaded)
{
SPContext.Current.FormContext.SetFormMode(SPControlMode.Edit, true);
SPContext.Current.ResetItem();
SPContext.Current.ItemId = itemId;
this.itemContext = SPContext.Current;
}
}
public bool TryRedirect()
{
try
{
if (this.itemContext != null && this.itemContext.Item != null)
{
return SPUtility.Redirect(this.ItemContext.RootFolderUrl, SPRedirectFlags.UseSource, HttpContext.Current);
}
}
catch (Exception ex)
{
// do something
throw ex;
}
finally
{
}
return false;
}
public bool TrySaveItem(bool uploadMode, string comments)
{
bool saved = false;
try
{
if (this.IsUploaded)
{
//The SaveButton has a static method called "SaveItem()" which you can use
//to kick the whole save process into high gear. Just right-click the method
//in Visuak Studio and select "Go to Definition" in the context menu to see
//all of the juicy details.
saved = SaveButton.SaveItem(this.ItemContext, uploadMode, comments);
if (saved)
{
this.OnItemSaved();
}
}
}
catch (Exception ex)
{
// do something
throw ex;
}
finally
{
}
return saved;
}
public bool TrySaveFile()
{
if (this.fileUpload.HasFile)
{
using (Stream uploadStream = this.fileUpload.FileContent)
{
this.OnFileUploading();
var originalFileName = this.fileUpload.FileName;
SPFile file = UploadFile(originalFileName, uploadStream);
var extension = Path.GetExtension(this.fileUpload.FileName);
this.itemId = file.Item.ID;
using (new EventFiringScope())
{
file.Item[SPBuiltInFieldId.ContentTypeId] = this.ContentTypeId;
file.Item.SystemUpdate(false);
//This code is used to guarantee that the file has a unique name.
var newFileName = String.Format("File{0}{1}", this.itemId, extension);
Folder = GetTargetFolder(file.Item);
if (!String.IsNullOrEmpty(Folder))
{
file.MoveTo(String.Format("{0}/{1}", Folder, newFileName));
}
file.Item.SystemUpdate(false);
}
this.ResetItemContext();
this.itemContext = SPContext.GetContext(HttpContext.Current, this.itemId, list.ID, list.ParentWeb);
this.OnFileUploaded();
return true;
}
}
return false;
}
public bool TryDeleteItem()
{
if (this.itemContext != null && this.itemContext.Item != null)
{
this.ItemContext.Item.Delete();
return true;
}
return false;
}
private SPFile UploadFile(string fileName, Stream uploadStream)
{
SPList list = SPContext.Current.List;
if (list == null)
{
throw new InvalidOperationException("The list or root folder is not specified.");
}
SPWeb web = SPContext.Current.Web;
SPFile file = list.RootFolder.Files.Add(fileName, uploadStream, true);
return file;
}
private string GetTargetFolder(SPListItem item)
{
var web = item.Web;
var rootFolder = item.ParentList.RootFolder.ServerRelativeUrl;
var subFolder = GetSubFolderBasedOnContentType(item[SPBuiltInFieldId.ContentTypeId]);
var folderPath = String.Format(@"{0}/{1}", rootFolder, subFolder);
var fileFolder = web.GetFolder(folderPath);
if (fileFolder.Exists) return folderPath;
return Folder;
}
private void OnFileUploading()
{
EventHandler handler = this.FileUploading;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
private void OnFileUploaded()
{
EventHandler handler = this.FileUploaded;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
private void OnItemSaving()
{
EventHandler handler = this.ItemSaving;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
private void OnItemSaved()
{
EventHandler handler = this.ItemSaved;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
然后我在我的“ CustomUpload ”类中使用它,这是我的ASPX页面的CodeBehind。
public partial class CustomUpload : LayoutsPageBase
{
#region Fields
private FileUploader uploader;
#endregion
#region Properties
public SPListItem CurrentItem { get; set; }
public SPContentType ContentType { get; set; }
public int DocumentID { get; set; }
private SPList List;
#endregion
public CustomUpload()
{
SPContext.Current.FormContext.SetFormMode(SPControlMode.New, true);
}
protected override void OnInit(EventArgs e)
{
if (IsPostBack)
{
// Get content type id from query string.
string contentTypeId = this.Request.QueryString["ContentTypeId"];
string folder = this.Request.QueryString["RootFolder"];
//ALL THE MAGIC HAPPENS HERE!!!
this.uploader = new FileUploader(SPContext.Current.List, this.NewFileUpload, contentTypeId, folder);
//These event handlers are CRITIAL! They are what enables you to perform the file
//upload, get the newly created ListItem, DocumentID and MOST IMPORTANTLY...
//the newly initialized ItemContext!!!
this.uploader.FileUploading += this.OnFileUploading;
this.uploader.FileUploaded += this.OnFileUploaded;
this.uploader.ItemSaving += this.OnItemSaving;
this.uploader.ItemSaved += this.OnItemSaved;
this.uploader.TrySaveFile();
}
base.OnInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{
//put in whatever custom code you want...
}
protected void OnSaveClicked(object sender, EventArgs e)
{
this.Validate();
var comments = Comments.Text;
if (this.IsValid && this.uploader.TrySaveItem(true, comments))
{
this.uploader.TryRedirect();
}
else
{
this.uploader.TryDeleteItem();
}
}
private void OnFileUploading(object sender, EventArgs e)
{
}
private void OnFileUploaded(object sender, EventArgs e)
{
//This is the next VERY CRITICAL piece of code!!!
//You need to retrieve a reference to the ItemContext that is created in the FileUploader
//class and then set your SPListFieldIterator's ItemContext equal to it.
this.MyListFieldIterator.ItemContext = this.uploader.ItemContext;
ContentType = this.uploader.ItemContext.ListItem.ContentType;
this.uploader.ItemContext.FormContext.SetFormMode(SPControlMode.Edit, true);
}
private void OnItemSaving(object sender, EventArgs e)
{
}
private void OnItemSaved(object sender, EventArgs e)
{
using (new EventFiringScope())
{
//This is where you could technically set any values for the ListItem that are
//not tied into any of your custom fields.
this.uploader.ItemContext.ListItem.SystemUpdate(false);
}
}
}
好的......那么这些代码的要点是什么?
如果你不热衷于实际查看我提供的评论,我会给你一个简短的总结。
代码的作用基本上是使用 FileUploader 帮助程序类执行整个文件上载过程,并使用附加到各种SPItem和SPFile相关事件的一系列 EventHandlers (即保存/保存和上载/上传)允许新创建的SPListItem和ItemContext对象以及SPListItem.Id值与CustomUpload类中使用的 SPContext.Current.ItemContext 同步。一旦你有了一个有效且新刷新的ItemContext,你可以“偷偷摸摸地deaky”设置你的 SPListFieldIterator (管理你的自定义字段)所使用的现有ItemContext等于在该中创建的ItemContext。 FileUpload 类并传递回 CustomUpload 类,该类实际上引用了新创建的ListItem!
此处需要注意的另一点是,您需要将 SPContext.Current.FormContext 和 SPListFieldIterator 的控制模式从“新建”设置为“编辑” ”。如果您不这样做,那么您将无法设置ItemContext和ListItem属性,并且您的数据将不会被保存。您也无法通过将控制模式值设置为“编辑”来启动,因为FormContext和SPListFieldIterator将期望一个现有的ItemContext,当然在初始页面或控件生命周期的任何一点都不会存在,因为你没有'实际上已经上传了文件!!!
所有上述代码必须从CustomUpload类的OnInit方法执行。这样做的原因是你可以将新创建的ItemContext注入SPListFieldIterator,然后再初始化它自己的子SPField控件(即你的CUSTOM CONTROLS !!!)。一旦SPListFieldIterator引用了新创建的ItemContext,它就可以使用所述ItemContext初始化它的所有子SPField控件, THAT 是如何使用自定义上载页面将FileUpload控件与自定义字段合并的以及一个或多个成功上传文件的SPListFieldIterators,并在单个原子操作中保存自定义字段中的所有值!
完成并完成!
注意:此解决方案不是“技术上”单一或剧烈操作,但它可以完成工作。