自定义sharepoint文件上传字段
Whilst doing a project for a client i came across an interesting need to be able to upload documents/files within a list. Almost like a filepicker control for a list. When i had a look around i quickly found what i was looking for here. The filepicker looks great, my one problem is I didnt have the money to buy it!
My recent interaction with custom content types got me thinking and upon more research i found it was possible to create a Field Control within Visual Studio. (provided you have WSS Extensions for Visual Studio installed). So i went ahead and added an “Empty” project under the sharepoint templates, and then proceeded to add a new item to the project, which gave me the option of adding a “Field Control”.
In similar fashion to the creation of a webpart, one is able to spit out controls and obviously the code behind them, which makes anything possible. Here is my code for the FilePicker class:
<div "="" id="highlighter_633012">{
private FieldControl1Field field;
private FileUpload fileUpload;
private HtmlTable table;
private string _td_path = “/BusinessUnits/IntHR”;
private string _DestUrlPath = string.Empty;
private string _DestFolder = string.Empty;
byte[] contents;
private SPSite _site = null;
private SPWeb _web = null;
public FilePicker(FieldControl1Field parentField)
{
this.field = parentField;
this.fileUpload = new FileUpload();
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
private string _uploadDocument(string theFile) {
try {
string serverName = “http://” + this.Page.Request.Url.Host;
SPSite site = new SPSite(serverName);
SPWeb web = site.OpenWeb();
SPUser user = web.AllUsers["VODACOM\\FARQVHCO"];
SPUserToken token = user.UserToken;
using (SPSite mySite = new SPSite(serverName + _td_path, token))
{
using (SPWeb myWeb = mySite.OpenWeb())
{
SPListItem myNewItem;
SPList list = myWeb.Lists["Photos"];
Stream fStream = fileUpload.PostedFile.InputStream;
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
SPFile newFile = list.RootFolder.Files.Add(Page.Request.QueryString["ID"].ToString(), contents,true);
//Get the file
return serverName + _td_path + “/Photos/” + fileUpload.FileName;
}
}
}
catch (Exception x)
{
throw new Exception(x.Message + “errorhere”);
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
this.table = new HtmlTable();
HtmlTableRow row = new HtmlTableRow();
table.Rows.Add(row);
HtmlTableCell cell = null;
if (this.ControlMode == SPControlMode.Edit || this.ControlMode == SPControlMode.New)
{
cell = new HtmlTableCell();
cell.ColSpan = 2;
cell.Attributes["class"] = “ms-formdescription”;
cell.InnerText = “Choose a file:”;
row = new HtmlTableRow();
table.Rows.Add(row);
cell = new HtmlTableCell();
row.Cells.Add(cell);
// Create a list selector.
this.fileUpload = new FileUpload();
cell.Controls.Add(this.fileUpload);
row.Cells.Add(cell);
}
base.Controls.Add(table);
}
public override void UpdateFieldValueInItem()
{
this.EnsureChildControls();
try
{
string uploaded = _uploadDocument(this.fileUpload.PostedFile.FileName);
this.Value = uploaded;
this.ItemFieldValue = this.Value;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
protected override void Render(HtmlTextWriter output)
{
this.table.RenderControl(output);
}
}
You’ll see i simply spit out a fileupload control and upload the file contents to a photos list. The hardcoding isnt great, but you get the idea to be able to create any type of control for you lists.
The main class simply spits out the filepicker control:
// TODO: Replace, as needed, “SPFieldText” with some other class derived from SPField.
// TODO: Update, as needed, ParentType element in fldtypes*.xml in this solution.
[CLSCompliant(false)]
[Guid("2a99cebe-f11c-488c-9717-0f674b5ab3a6")]
public class FieldControl1Field : SPFieldText
{
public FieldControl1Field(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{
}
public FieldControl1Field(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{
}
public override BaseFieldControl FieldRenderingControl
{
[SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
get
{
BaseFieldControl fieldControl = new FilePicker(this);
fieldControl.FieldName = this.InternalName;
return fieldControl;
}
}
}
Then the xml which is included in the templates section looks a little like this msdn article (see the bottom of the page). In actual fact follow that article too, its a great help!
well hope this helps
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
2010-07-20 SharePoint2007 配置MOSS基于AD的Forms验证