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

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


内容来源:

http://www.sharepointchina.com  sharepoint中文网

平台是以sharepoint为中心,延伸到NET开发,前端技术,希望大家多多支持。邀请各位同行入驻。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值