自定义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">
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 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

 

posted @ 2012-07-20 20:44  架构师聊技术  阅读(211)  评论(0编辑  收藏  举报