An example of “how to create a consumer webpart”.
Description: This webpart can be connected to a list or a library webpart. The selected item in the provider webpart will be shown in an iframe.
Custom Settings: Define if the webpart consumes from a library or a list webpart.
Screenshot:
Custom Webpart Setting:
Project Structure
Code FileViewerConsumerWebpart.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
using System; using System.ComponentModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Data; namespace SPLoupe.FilePreviewConsumer.FileViewerConsumerWebpart { [ToolboxItemAttribute(false)] public class FileViewerConsumerWebpart : WebPart { public static string FileToShow { get; set; } public enum ProviderWebpartChoice { Library, List } [WebBrowsable(true), WebDisplayName("Provider Webpart:"), Category("Custom Settings"), Personalizable(PersonalizationScope.Shared), Description("Choose if you connect this webpart from a list or a library"),] public ProviderWebpartChoice ProviderType { get; set; } private IWebPartRow _myProvider; private DataRowView _DataRow; LiteralControl iframe = new LiteralControl("<iframe id='iframe' runat='server' src=" + FileToShow + "' style='width: 100%; height: 90%;'></iframe>"); [ConnectionConsumer("Row")] public void SetConnectionInterface(IWebPartRow provider) { _myProvider = provider; } public void GetRowData(object rowData) { EnsureChildControls(); _DataRow = (DataRowView)rowData; if (ProviderType == ProviderWebpartChoice.Library) { using (SPSite site = new SPSite(SPContext.Current.Web.Url)) { using (SPWeb web = site.OpenWeb()) { try { if (_DataRow["ContentType"].ToString() != "Folder" && _DataRow["ContentType"].ToString() != "Ordner") { FileToShow = _DataRow["DocUrl"].ToString(); /*findout the property names foreach (DataColumn v in _DataRow.Row.Table.Columns) { FileToShow = FileToShow+"\nCollumnsValue: " + v.ToString(); }*/ } else { FileToShow = string.Empty; } } catch (Exception) { } } } } else if (ProviderType == ProviderWebpartChoice.List) { try { FileToShow = _DataRow["URL"].ToString().Substring(0, _DataRow["URL"].ToString().IndexOf(',')); } catch (Exception) { } } //Show FileToShow //LiteralControl LabelPath = new LiteralControl(); //LabelPath.Text = "FileToShow URL = " + FileToShow; //Controls.Add(LabelPath); iframe.Text = "<iframe id='iframe' runat='server' src='" + FileToShow + "' style='width: 100%; height: 90%;'></iframe>"; Controls.Add(iframe); } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (_myProvider != null) { _myProvider.GetRowData(new RowCallback(GetRowData)); } } protected override void CreateChildControls() { } } } |