自定义sharepoint文件上传字段
来源:互联网 发布:卡通农场最新数据 编辑:程序博客网 时间:2024/05/17 18:44
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 forhere. 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 haveWSS 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:
public class FilePicker : TextField{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 filereturn 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
内容来源:
http://www.sharepointchina.com sharepoint中文网
平台是以sharepoint为中心,延伸到NET开发,前端技术,希望大家多多支持。邀请各位同行入驻。- 自定义sharepoint文件上传字段
- sharepoint 创建自定义字段
- 开发自定义字段类型 sharepoint
- SharePoint 2010 自定义字段开发
- SharePoint 2010 自定义字段开发
- 上传文件至sharepoint
- C#代码上传文件到SharePoint文档库并给字段赋值
- SharePoint 2010 自定义字段"大写金额"开发
- SharePoint 2010 自定义 字段 类型--------省市区联动
- sharepoint 2013 开发自定义字段类型说明
- SharePoint 2016 自定义城市和区域字段
- SHAREPOINT 2013 - 批量上传文件
- Sharepoint项目周记三:自定义上传页面等
- Sharepoint项目:自定义上传页面等
- SharePoint 2010 自定义 字段 类型--------三级互联(省县市)
- SharePoint 2013 图文开发系列之自定义字段
- sharepoint 2010 自定义字段开发(1) 创建一个简单的列表自定义字段
- 文件上传自定义样式
- Greedy Change
- 唯一值符号化
- 第四章 Xtreme Toolkit Pro v13.2 使用指南
- poj 1043
- JBPM4入门+程序下载
- 自定义sharepoint文件上传字段
- 桥接模式(Bridge Pattern)
- ubuntu12.04安装中文输入法(ibus)
- 随机模拟的基本思想和常用采样方法(sampling)
- Java垃圾回收机制小结
- Multi-statement transaction required more than 'max_binlog_cache_size' bytes of storage
- sql server获取sql语句执行的毫秒数
- BFS_连连看游戏 一个让人感觉很复杂的BFS
- DirectX 11 SDK文档(一)