<?xml version="1.0" encoding="utf-8" ?> <!-- , --> <FieldTypes> <!-- --> <FieldType> <!-- CAML--> <Field Name="TypeName">SPFileField</Field> <!-- ( /custom-) --> <Field Name="ParentType">Text</Field> <!-- SharePoint --> <Field Name="TypeDisplayName">SPFileField</Field> <!-- --> <Field Name="TypeShortDescription">SPFileField</Field> <!-- --> <Field Name="UserCreatable">TRUE</Field> <!-- , --> <Field Name="ShowOnListCreate">TRUE</Field> <Field Name="ShowOnSurveyCreate">TRUE</Field> <Field Name="ShowOnDocumentLibraryCreate">TRUE</Field> <Field Name="ShowOnColumnTemplateCreate">TRUE</Field> <!--, --> <Field Name="Sortable">FALSE</Field> <Field Name="Filterable">FALSE</Field> <!-- , (Text) --> <Field Name="AllowBaseTypeRendering">TRUE</Field> <!-- ( ) --> <Field Name="FieldTypeClass">SPFileFieldControl.SPFileField, $SharePoint.Project.AssemblyFullName$</Field> <!-- , . --> <PropertySchema> <Fields> <!-- , , , --> <Field Name="LibraryName" DisplayName=" " Type="Text" Required="TRUE" /> </Fields> </PropertySchema> </FieldType> </FieldTypes>
public class FileValue : SPFieldMultiColumnValue { private const int c_PropNumber = 3; public FileValue() : base(c_PropNumber) {} public FileValue(string value) : base(value) {} public string Name { get { return base[0]; } set { base[0] = value; } } public string Url { get { return base[1]; } set { base[1] = value; } } public Guid _UniqueID = Guid.Empty; public Guid UniqueID { get { if (_UniqueID == Guid.Empty) _UniqueID = new Guid(base[2]); return _UniqueID; } set { _UniqueID = value; base[2] = value.ToString(); } } }
public class SPFileField : SPField { // SPField public SPFileField(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { } public SPFileField(SPFieldCollection fields, string typeName, string displayName) : base(fields, typeName, displayName) { } public override BaseFieldControl FieldRenderingControl { get { // string libraryName = Convert.ToString(this.GetCustomProperty("LibraryName")); // BaseFieldControl ctrl = new SPFileFieldControl(libraryName); ctrl.FieldName = this.InternalName; return ctrl; } } // FileValue public override object GetFieldValue(string value) { if (String.IsNullOrEmpty(value)) return null; return new FileValue(value); } }
public class SPFileFieldControl : BaseFieldControl { // , private const string fileNotExist = " "; // private const string editTemplateName = "SPFileFieldControlEdit"; // private const string displayTemplateName = "SPFileFieldControlDisplay"; // , public string libraryName; // Value, FileValue private FileValue currentFile = null; public override object Value { get { return currentFile; } set { currentFile = (FileValue)value; } } public SPFileFieldControl(string aLibraryName) { libraryName = aLibraryName; } // protected override void OnInit(EventArgs e) { currentFile = (FileValue)this.ItemFieldValue; base.OnInit(e); } // , protected override string DefaultTemplateName { get { return base.ControlMode == SPControlMode.Display ? displayTemplateName : editTemplateName; } } public override string DisplayTemplateName { get { return displayTemplateName; } } // protected override void CreateChildControls() { base.CreateChildControls(); if (base.ControlMode == SPControlMode.Display) { SetupDisplayTemplateControls(); } else { SetupEditTemplateControls(); } } // private void SetupEditTemplateControls() { FileUpload fuDocument = (FileUpload)TemplateContainer.FindControl("fuDocument"); HyperLink aFile = (HyperLink)TemplateContainer.FindControl("aFile"); HiddenField hdFileName = (HiddenField)TemplateContainer.FindControl("hdFileName"); HtmlInputImage btnDelete = (HtmlInputImage)TemplateContainer.FindControl("btnDelete"); HtmlInputImage btnAdd = (HtmlInputImage)TemplateContainer.FindControl("btnAdd"); // if (currentFile != null) { // SPWeb web = SPContext.Current.Site.RootWeb; SPList list = web.GetList(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, libraryName)); SPListItem spFileItem = list.GetItemByUniqueId(currentFile.UniqueID); // aFile.NavigateUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, spFileItem.Url); aFile.Text = currentFile.Name; // UniqueId hdFileName.Value = spFileItem.UniqueId.ToString(); } else { // , aFile.Text = fileNotExist; hdFileName.Value = String.Empty; } btnDelete.Attributes.Add("onclick", String.Format(@"clearFileValue('{0}','{1}','{2}','{3}');return false;", aFile.ClientID, fuDocument.ClientID, hdFileName.ClientID, fileNotExist)); btnAdd.Attributes.Add("onclick", String.Format(@"changeDisplay('{0}');return false;", fuDocument.ClientID)); fuDocument.Attributes.Add("onchange",String.Format(@"changeFileName(this,'{0}');return false;", aFile.ClientID)); } // private void SetupDisplayTemplateControls() { if (currentFile != null) { // SPWeb web = SPContext.Current.Site.RootWeb; SPList list = web.GetList(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, libraryName)); SPListItem spFileItem = list.GetItemByUniqueId(currentFile.UniqueID); // HyperLink aFile = (HyperLink)TemplateContainer.FindControl("aFile"); aFile.NavigateUrl = SPUrlUtility.CombineUrl(web.ServerRelativeUrl, spFileItem.Url); aFile.Text = currentFile.Name; } } // public override void UpdateFieldValueInItem() { // Page.Validate(); if (Page.IsValid) { // , FileUpload fuDocument = (FileUpload)TemplateContainer.FindControl("fuDocument"); HiddenField hdFileName = (HiddenField)TemplateContainer.FindControl("hdFileName"); // , , if (hdFileName.Value == String.Empty && currentFile != null) { // deleteFile(); // currentFile = null; } // if (fuDocument.HasFile) { SPWeb web = SPContext.Current.Site.RootWeb; SPFolder folder = web.GetFolder(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, libraryName)); // , if (currentFile != null) { deleteFile(); } // currentFile = addFile(folder, fuDocument); } base.UpdateFieldValueInItem(); } } // private void deleteFile() { // UniqueID SPWeb web = SPContext.Current.Site.RootWeb; SPList list = web.GetList(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, libraryName)); SPListItem spFileItem = list.GetItemByUniqueId(currentFile.UniqueID); spFileItem.Delete(); } // private FileValue addFile(SPFolder folder, FileUpload fuDocument) { string uniqueName = Guid.NewGuid().ToString(); // uniqueName SPFile spfile = folder.Files.Add(uniqueName, fuDocument.FileContent,true); // spfile.Item["BaseName"] = String.Format("{1}-{0}", fuDocument.FileName, spfile.Item.ID); spfile.Item.Update(); return new FileValue() { Url = spfile.Item.Url, UniqueID = spfile.Item.UniqueId, Name = fuDocument.FileName }; } }
<%-- --%> <SharePoint:RenderingTemplate ID="SPFileFieldControlEdit" runat="server"> <Template> <%-- CSS JS --%> <SharePoint:CssRegistration ID="CssRegistration1" name="/_layouts/styles/FileFieldControl.css" after="corev4.css" runat="server"/> <SharePoint:ScriptLink ID="ScriptLink1" runat="server" Name="FileFieldControl.js" Localizable="false"/> <%-- --%> <asp:HyperLink ID="aFile" runat="server" CssClass="ffc-hl" EnableViewState="False"/> <%-- - ) --%> <input type="image" ID="btnDelete" runat="server" src="/_layouts/images/DELETE.gif" alt="delete"/> <%-- - FileUpload --%> <input type="image" ID="btnAdd" runat="server" src="/_layouts/images/newrowheader.png" alt="add"/> <br/> <asp:FileUpload ID="fuDocument" runat="server" CssClass="ffc-fu" /> <%-- UniqueId --%> <asp:HiddenField runat="server" ID="hdFileName"/> </Template> </SharePoint:RenderingTemplate> <%-- --%> <SharePoint:RenderingTemplate ID="SPFileFieldControlDisplay" runat="server"> <Template> <%-- CSS --%> <SharePoint:CssRegistration ID="CssRegistration1" name="/_layouts/styles/FileFieldControl.css" after="corev4.css" runat="server"/> <%-- --%> <asp:HyperLink ID="aFile" runat="server" CssClass="ffc-hl" EnableViewState="False"/> </Template> </SharePoint:RenderingTemplate>
// / function changeDisplay(id) { var v = document.getElementById(id); if (v.style.display == 'block') //|| v.style.display == '') { v.style.display = 'none'; } else { v.style.display = 'block'; } } // , function clearFileValue(aID, fID, hfId, defText) { var a = document.getElementById(aID); a.innerText = defText; a.removeAttribute("href"); var hf = document.getElementById(hfId); hf.value = ''; var f = document.getElementById(fID); f.outerHTML = f.outerHTML; } // function changeFileName(oFile, aID) { var a = document.getElementById(aID); var fullPath = oFile.value; if (fullPath) { // var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/')); var filename = fullPath.substring(startIndex); if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) { filename = filename.substring(1); } a.innerText = filename; } }
.ffc-hl { text-align:center; vertical-align:top; } .ffc-fu { margin-top: 3px; display: none; }
<!-- SPFileField--> <xsl:template match="FieldRef[@FieldType='SPFileField']" mode="Text_body"> <!-- --> <xsl:param name="thisNode" select="."/> <xsl:variable name="full" select="$thisNode/@*[name()=current()/@Name]" /> <!-- , ( UniqueID, ) --> <!-- "" --> <xsl:variable name="name" select="substring-before(substring-after($full,';#'),';#')" /> <xsl:variable name="url" select="substring-before(substring-after(substring-after($full,';#'),';#'),';#')" /> <!-- --> <xsl:element name="a"> <xsl:attribute name="href"> <xsl:value-of select="concat($RootSiteUrl,'/',$url)"/> </xsl:attribute> <xsl:value-of select="$name"/> </xsl:element> </xsl:template>
<?xml version="1.0" encoding="utf-8"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Receivers ListUrl="Lists/TestList"> <Receiver> <Name>ER_OnItemDeletingItemDeleting</Name> <Type>ItemDeleting</Type> <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly> <Class>SPFileFieldControl.ER_OnItemDeleting.ER_OnItemDeleting</Class> <SequenceNumber>10000</SequenceNumber> </Receiver> </Receivers> </Elements>
public class ER_OnItemDeleting : SPItemEventReceiver { public const string ER_OnItemDeletingName = "SPFileFieldControl.ER_OnItemDeleting.ER_OnItemDeleting"; public const string SPFileFieldName = "SPFileField"; public override void ItemDeleting(SPItemEventProperties properties) { // for (int i = 0; i < properties.ListItem.Fields.Count; i++) { // SPFileField if (properties.ListItem.Fields[i].TypeAsString == ER_OnItemDeleting.SPFileFieldName) { // FileValue fileValue = properties.ListItem[properties.ListItem.Fields[i].StaticName] as FileValue; if (fileValue == null) continue; // SPFile spFile = properties.Web.GetFile(fileValue.UniqueID); spFile.Delete(); } } base.ItemDeleting(properties); } }
public class ER_OnFileFieldCRUD : SPListEventReceiver { public const string SPFileFieldName = "SPFileField"; public override void FieldAdded(SPListEventProperties properties) { // SPFileField, , if (properties.Field.TypeDisplayName == SPFileFieldName) { // EventReceiver, bool eventReceiverExists = false; for (int i = 0; i < properties.List.EventReceivers.Count;i++) { if (properties.List.EventReceivers[i].Class == ER_OnItemDeleting.ER_OnItemDeleting.ER_OnItemDeletingName) { eventReceiverExists = true; break; } } if (!eventReceiverExists) { properties.List.EventReceivers.Add(SPEventReceiverType.ItemDeleting, Assembly.GetExecutingAssembly().FullName, ER_OnItemDeleting.ER_OnItemDeleting.ER_OnItemDeletingName); } } base.FieldAdded(properties); } public override void FieldDeleting(SPListEventProperties properties) { if (properties.Field.TypeDisplayName == SPFileFieldName) { // SPFileField, // 1) , for (int i = 0; i < properties.List.Items.Count; i++) { SPListItem splistItem = properties.List.Items[i]; FileValue fileValue = splistItem[properties.Field.StaticName] as FileValue; if (fileValue == null) continue; SPFile spFile = properties.Web.GetFile(fileValue.UniqueID); spFile.Delete(); } // , SPFileField bool anyFileFieldExist = false; for (int i = 0; i < properties.List.Fields.Count; i++) { if (properties.List.Fields[i].TypeAsString == SPFileFieldName && properties.List.Fields[i].StaticName != properties.Field.StaticName) { anyFileFieldExist = true; break; } } // 2) , SPFileField if (!anyFileFieldExist) { for (int i = 0; i < properties.List.EventReceivers.Count; ) { if (properties.List.EventReceivers[i].Class == ER_OnItemDeleting.ER_OnItemDeleting.ER_OnItemDeletingName) { properties.List.EventReceivers[i].Delete(); } else { i++; } } } } base.FieldDeleting(properties); } }
Source: https://habr.com/ru/post/235493/
All Articles