自定义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开发,前端技术,希望大家多多支持。邀请各位同行入驻。
原创粉丝点击