Upload methods (III)

Source: Internet
Author: User

The last two articles describe simple usage of built-in SharePoint upload. This article describes how to develop custom Field Types for upload. I wrote all the code last night (with bugs) without using the company's original upload parts. The effect is as follows:

 

View status chart:

 

It only implements the simple upload function. Of course, it can be expanded and improved.

The following documents are used:

1. fldtypes_fieldupload.xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<FieldTypes>
<FieldType>
<Field Name = "TypeName"> FieldUpload </Field>
<Field Name = "ParentType"> Note </Field>
<Field Name = "TypeDisplayName"> upload </Field>
<Field Name = "TypeShortDescription"> upload </Field>

<Field Name = "UserCreatable"> TRUE </Field>
<Field Name = "ShowInListCreate"> TRUE </Field>
<Field Name = "ShowInSurveyCreate"> TRUE </Field>
<Field Name = "show.cumentlibrarycreate"> TRUE </Field>
<Field Name = "ShowInColumnTemplateCreate"> TRUE </Field>
<Field Name = "FieldTypeClass"> SharePointProject1.FileUploadCustomField, $ SharePoint. Project. AssemblyFullName $ </Field>
<Field Name = "FieldEditorUserControl">/_ controltemplates/FileUploadProperty. ascx </Field>
<PropertySchema>
<Fields>
<Field Name = "UploadDocumentLibrary" DisplayName = "Document Library" Type = "Text" Hidden = "True"/>
</Fields>
</PropertySchema>
</FieldType>
</FieldTypes>

2. FileUploadCustomField. cs

Public class FileUploadCustomField: SPFieldMultiLineText
{
Public FileUploadCustomField (SPFieldCollection fields, string fieldName): base (fields, fieldName) {Init ();}
Public FileUploadCustomField (SPFieldCollection fields, string typeName, string disName): base (fields, typeName, disName) {Init ();}

Private string _ UploadDocumentLibrary = "";
Public string UploadDocumentLibrary
{
Get
{
Return _ UploadDocumentLibrary;
}
Set
{
This. SetCustomProperty ("UploadDocumentLibrary", value );
_ UploadDocumentLibrary = value;
}
}

Private void Init ()
{
This. UploadDocumentLibrary = this. GetCustomProperty ("UploadDocumentLibrary"). ToStr ();
}

Public override void Update ()
{
This. SetCustomProperty ("UploadDocumentLibrary", this. UploadDocumentLibrary );
This. RichText = true;
This. RichTextMode = SPRichTextMode. FullHtml;
Base. Update ();
}

Public override BaseFieldControl FieldRenderingControl
{
Get
{
BaseFieldControl fieldControl = new FileUploadCustomFieldControl ();
FieldControl. FieldName = InternalName;
Return fieldControl;
}
}

Public override string GetFieldValueAsHtml (object value)
{
Return base. GetFieldValueAsHtml (HttpUtility. HtmlDecode (value. ToStr ()));
}


}

3. FileUploadCustomFieldControl. cs

{
FileUpload fileup = null;
Button btn = null;
Literal litWarn = null;

 

Protected override void CreateChildControls ()
{
If (this. ControlMode! = SPControlMode. Display)
{
HtmlTable htable = new HtmlTable ();

Fileup = new FileUpload ();

// HttpContext. GetGlobalResourceObject ("myCustom", "fileup-btn"). ToString ();

String btnText = SPUtility. GetLocalizedString ("$ Resources: myCustom, fileup_btn;", "myCustom", SPContext. Current. Web. Language );

Btn = new Button ();
Btn. Text = btnText;
Btn. Click + = new EventHandler (btn_Click );
LitWarn = new Literal ();

 

HtmlTableCell btncell = new HtmlTableCell ();
Btncell. Controls. Add (fileup );
Btncell. Controls. Add (btn );
Btncell. Controls. Add (litWarn );

HtmlTableRow btnrow = new HtmlTableRow ();

Btnrow. Cells. Add (btncell );
Htable. Rows. Add (btnrow );

This. Controls. Add (htable );
}

Base. CreateChildControls ();
}

Public override object Value
{
Get
{
This. EnsureChildControls ();

Return ViewState ["filepath"];
}
Set
{
This. EnsureChildControls ();

AddFilePath (this. ItemFieldValue. ToStr ());
}
}

Protected void btn_Click (object sender, EventArgs e)
{
LitWarn. Text = "";

If (fileup. FileName = "")
{
If (ViewState ["filepath"]! = Null)
{
AddFilePath (ViewState ["filepath"]. ToStr ());
}

LitWarn. Text = "file not selected ";
Return;
}

Stream st = fileup. FileContent;

FileUploadCustomField _ field = (FileUploadCustomField) this. Field;

SPList list = SPContext. Current. Web. GetListFromUrl (_ field. UploadDocumentLibrary );

SPDocumentLibrary spdoc = list as SPDocumentLibrary;

SPFile fl = spdoc. RootFolder. Files. Add (fileup. FileName, st, true );

If (fl! = Null)
{
String filepath = "<a href = '" + fl. ServerRelativeUrl + "'>" + fileup. FileName + "</a> ";

AddFilePath (filepath );

}
}

Private void AddFilePath (string filepath)
{
HtmlTable htable = new HtmlTable ();

HtmlTableCell filecell = new HtmlTableCell ();

Filecell. Controls. Add (new LiteralControl (filepath ));

HtmlTableRow filerow = new HtmlTableRow ();
Filerow. Cells. Add (filecell );

HtmlTableCell delcell = new HtmlTableCell ();

LinkButton lbtn = new LinkButton ();
Lbtn. Text = "delete ";
Lbtn. Click + = new EventHandler (lbtn_Click );

Delcell. Controls. Add (lbtn );
Filerow. Cells. Add (delcell );

Htable. Rows. Add (filerow );

ViewState ["filepath"] = filepath;

This. Controls. Add (htable );
}

Void lbtn_Click (object sender, EventArgs e)
{

 

}

Protected override void RenderFieldForDisplay (System. Web. UI. HtmlTextWriter output)
{
Output. Write (this. ItemFieldValue );
}
}

4. FileUploadProperty. ascx

<% @ Register TagPrefix = "wssuc" TagName = "InputFormControl" Src = "~ /_ Controltemplates/InputFormControl. ascx "%>
<% @ Register TagPrefix = "wssuc" TagName = "InputFormSection" Src = "~ /_ Controltemplates/InputFormSection. ascx "%>
<Wssuc: InputFormSection runat = "server" id = "UploadControl" Title = "attribute settings">
<Template_inputformcontrols>
<Wssuc: InputFormControl runat = "server" LabelText = "Select document library">
<Template_Control>
<Asp: DropDownList ID = "ddlDocLibs" runat = "server" CssClass = "ms-ButtonheightWidth" Width = "250px"/>
</Template_Control>
</Wssuc: InputFormControl>
</Template_inputformcontrols>
</Wssuc: InputFormSection>

5. FileUploadProperty. ascx. cs

Public partial class FileUploadProperty: UserControl, IFieldEditor
{
Protected void Page_Load (object sender, EventArgs e)
{

}

FileUploadCustomField _ field = null;

Public bool DisplayAsNewSection
{
Get {return true ;}
}

Public void InitializeWithField (SPField field)
{
This. _ field = field as FileUploadCustomField;
}

Public void OnSaveChange (SPField field, bool isNewField)
{
FileUploadCustomField myField = field as FileUploadCustomField;
MyField. UploadDocumentLibrary = ddlDocLibs. SelectedItem. Value;
}

Protected override void CreateChildControls ()
{
Base. CreateChildControls ();
SPListCollection objLists = SPContext. Current. Web. Lists;
Foreach (SPList objList in objLists)
{
If (objList is SPDocumentLibrary)
{
DdlDocLibs. Items. Add (new ListItem (objList. Title, objList. DefaultViewUrl ));
}
}

If (! IsPostBack & _ field! = Null)
{
If (! String. IsNullOrEmpty (_ field. UploadDocumentLibrary ))
{
Foreach (ListItem item in ddlDocLibs. Items)
{
Item. Selected = false;
}

DdlDocLibs. Items. FindByValue (_ field. UploadDocumentLibrary). Selected = true;
}
}
}
}

After the upload is complete, click Save.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.