Condividi tramite


Code Snippet: Custom External Data Part

Applies to: SharePoint Server 2010

The following example shows the implementation of a custom external data part.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.BusinessApplications.Runtime.UI;
using Microsoft.BusinessData.Runtime;
using Microsoft.Office.BusinessData.Offlining;
using Microsoft.BusinessData.Offlining;
using outlookPIA = Microsoft.Office.Interop.Outlook;
using Microsoft.BusinessData.MetadataModel;

namespace ContosoCustomer
{
    public partial class CustomOBPart :WinFormsOBPartBase //UserControl
    {
        private IEntityInstance SelectedOrderEntityInstance;
        private IEntityInstance CurrentCutomerEntityInstance;
        private List<IEntityInstance> RelatedOrderEntityInstanceList = new List<IEntityInstance>();
        
        public CustomOBPart()   
        {
            InitializeComponent();  
        }

        //Is triggered after Entity instance is set as data source into this OBPart.
        protected override void OnDataSourceChanged(Microsoft.Office.BusinessApplications.Model.DataSourceChangedEventArgs args)
        {
            base.OnDataSourceChanged(args);

            //Retrieve Entity instance that was passed in as DataSource,
            CurrentCutomerEntityInstance = this.DataSource as IEntityInstance;

            //Populate drop-down list for related orders.
            PopulateRelateOrderList();
        }

        //Is triggered when Item is saved.
        protected override void SaveCore()
        {
            if (IsDirtyCore)
            {
                //Update item if there is change in OBPart.
                BTUpdateOrder_Click(null, null);
            }
            base.SaveCore();
        }

        //Is triggered when the inspector of the current item is closed.
        protected override bool IsDirtyCore
        {
            get
            {
                if (SelectedOrderEntityInstance == null)
                {
                    return false;
                }
                //Check whether the values on OBPart have been changed.
                return
                    (DateTime)SelectedOrderEntityInstance["OrderDate"]!= this.DTOrderDate.Value.ToUniversalTime() ||
                        SelectedOrderEntityInstance["SubTotal"].ToString() != this.TBSubtotal.Text ||
                        SelectedOrderEntityInstance["TaxAmt"].ToString() != this.TBTaxAmount.Text ||
                        SelectedOrderEntityInstance["TotalDue"].ToString() != this.TBTotalDue.Text;
            }
        }

        private void BTUpdateOrder_Click(object sender, EventArgs e)
        {
            if (SelectedOrderEntityInstance == null)
            {
                MessageBox.Show("Can not find IEntityInstance");
                return;
            }
            //Set updated values.
            SelectedOrderEntityInstance["OrderDate"] = this.DTOrderDate.Value.ToUniversalTime();
            SelectedOrderEntityInstance["SubTotal"] = Convert.ToDecimal(this.TBSubtotal.Text);
            SelectedOrderEntityInstance["TaxAmt"] = Convert.ToDecimal(this.TBTaxAmount.Text);
            SelectedOrderEntityInstance["TotalDue"] = Convert.ToDecimal(this.TBTotalDue.Text);
            //Update entity reference.
            SelectedOrderEntityInstance.Update();

            //Trigger Sync Now for all Order Entities.
            TriggerSyncNow();
            //Populate Related Order list.
            PopulateRelateOrderList();
        }

        private void CBRelatedOrederList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SelectedOrderEntityInstance = this.RelatedOrderEntityInstanceList[CBRelatedOrederList.SelectedIndex];
            PopulateSelectedEntityInstance();
        }

        private void PopulateRelateOrderList()
        {
            this.CBRelatedOrederList.Items.Clear();
            this.RelatedOrderEntityInstanceList.Clear();

            //Read related order list.
            IEntityInstanceEnumerator instanceEnumerator = null;
            try
            {
                IEntity sourceEntity = CurrentCutomerEntityInstance.Entity;
                IAssociation association = sourceEntity.GetSourceAssociations()["GetOrdersForCustomer"];
                IFilterCollection filters = association.GetFilters();
                IEntity DestEntity = association.GetDestination();
                ILobSystem DestLobSystem = DestEntity.GetLobSystem();
                ILobSystemInstance DestLobSystemInstance = DestLobSystem.GetLobSystemInstances()["http://cox64-185:8080/webservice.asmx?wsdl"];
                EntityInstanceCollection sourceEntityCollection = new EntityInstanceCollection();
                sourceEntityCollection.Add(CurrentCutomerEntityInstance);
                instanceEnumerator = DestEntity.FindAssociated(sourceEntityCollection, association, DestLobSystemInstance);

                while (instanceEnumerator.MoveNext())
                {
                    IEntityInstance currentEntity = instanceEnumerator.Current;
                    this.CBRelatedOrederList.Items.Add(((DateTime)currentEntity["OrderDate"]).ToShortDateString() + " - " + currentEntity["TotalDue"]);
                    this.RelatedOrderEntityInstanceList.Add(currentEntity);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Failed to find related Orders");
            }
            finally
            {
                instanceEnumerator.Close();
            }
            if (this.CBRelatedOrederList.Items.Count > 0)
            {
                this.CBRelatedOrederList.SelectedIndex = 0;
            }
        }

        private void TriggerSyncNow()
        {
            
            RemoteOfflineRuntime offlineRuntime = new RemoteOfflineRuntime();
            ISubscriptionManager subManager = offlineRuntime.GetSubscriptionManager();
            ISubscription sub = subManager.GetSubscription("AWWSExample", "OrderHeader", "GetSalesOrderHeaderById", "AWWSExampleOrderHeaderSubscription");
            sub.RequestRefresh(true);
         }

        private void PopulateSelectedEntityInstance()
        {
            this.TBOrderID.Text = SelectedOrderEntityInstance.GetIdentity().GetIdentifierValues().GetValue(0).ToString();
            this.DTOrderDate.Value = ((DateTime)SelectedOrderEntityInstance["OrderDate"]).ToLocalTime();
            this.TBSubtotal.Text = SelectedOrderEntityInstance["SubTotal"].ToString();
            this.TBTaxAmount.Text = SelectedOrderEntityInstance["TaxAmt"].ToString();
            this.TBTotalDue.Text = SelectedOrderEntityInstance["TotalDue"].ToString();
        }
    }
}

The following is the autogenerated code (from CustomOBPart.Designer.cs) that corresponds to the external data part.

namespace ContosoCustomer
{
    partial class CustomOBPart
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.LBOrderID = new System.Windows.Forms.Label();
            this.TBOrderID = new System.Windows.Forms.TextBox();
            this.LBRelatedOrderTitle = new System.Windows.Forms.Label();
            this.LBOrderDate = new System.Windows.Forms.Label();
            this.DTOrderDate = new System.Windows.Forms.DateTimePicker();
            this.TBSubtotal = new System.Windows.Forms.TextBox();
            this.LBSubTotal = new System.Windows.Forms.Label();
            this.TBTaxAmount = new System.Windows.Forms.TextBox();
            this.LBTaxAmount = new System.Windows.Forms.Label();
            this.TBTotalDue = new System.Windows.Forms.TextBox();
            this.LBTotalDue = new System.Windows.Forms.Label();
            this.BTUpdateOrder = new System.Windows.Forms.Button();
            this.LBRelatedOrderList = new System.Windows.Forms.Label();
            this.CBRelatedOrederList = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
            // 
            // LBOrderID
            // 
            this.LBOrderID.AutoSize = true;
            this.LBOrderID.ForeColor = System.Drawing.Color.MidnightBlue;
            this.LBOrderID.Location = new System.Drawing.Point(3, 108);
            this.LBOrderID.Name = "LBOrderID";
            this.LBOrderID.Size = new System.Drawing.Size(50, 13);
            this.LBOrderID.TabIndex = 0;
            this.LBOrderID.Text = "Order ID:";
            // 
            // TBOrderID
            // 
            this.TBOrderID.Enabled = false;
            this.TBOrderID.Location = new System.Drawing.Point(71, 105);
            this.TBOrderID.Name = "TBOrderID";
            this.TBOrderID.Size = new System.Drawing.Size(119, 20);
            this.TBOrderID.TabIndex = 1;
            // 
            // LBRelatedOrderTitle
            // 
            this.LBRelatedOrderTitle.AutoSize = true;
            this.LBRelatedOrderTitle.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.LBRelatedOrderTitle.ForeColor = System.Drawing.Color.DarkRed;
            this.LBRelatedOrderTitle.Location = new System.Drawing.Point(3, 82);
            this.LBRelatedOrderTitle.Name = "LBRelatedOrderTitle";
            this.LBRelatedOrderTitle.Size = new System.Drawing.Size(119, 15);
            this.LBRelatedOrderTitle.TabIndex = 2;
            this.LBRelatedOrderTitle.Text = "Related Order Detail";
            // 
            // LBOrderDate
            // 
            this.LBOrderDate.AutoSize = true;
            this.LBOrderDate.ForeColor = System.Drawing.Color.MidnightBlue;
            this.LBOrderDate.Location = new System.Drawing.Point(3, 134);
            this.LBOrderDate.Name = "LBOrderDate";
            this.LBOrderDate.Size = new System.Drawing.Size(62, 13);
            this.LBOrderDate.TabIndex = 3;
            this.LBOrderDate.Text = "Order Date:";
            // 
            // DTOrderDate
            // 
            this.DTOrderDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
            this.DTOrderDate.Location = new System.Drawing.Point(72, 126);
            this.DTOrderDate.Name = "DTOrderDate";
            this.DTOrderDate.Size = new System.Drawing.Size(118, 20);
            this.DTOrderDate.TabIndex = 4;
            // 
            // TBSubtotal
            // 
            this.TBSubtotal.Location = new System.Drawing.Point(72, 152);
            this.TBSubtotal.Name = "TBSubtotal";
            this.TBSubtotal.Size = new System.Drawing.Size(119, 20);
            this.TBSubtotal.TabIndex = 6;
            // 
            // LBSubTotal
            // 
            this.LBSubTotal.AutoSize = true;
            this.LBSubTotal.ForeColor = System.Drawing.Color.MidnightBlue;
            this.LBSubTotal.Location = new System.Drawing.Point(4, 155);
            this.LBSubTotal.Name = "LBSubTotal";
            this.LBSubTotal.Size = new System.Drawing.Size(56, 13);
            this.LBSubTotal.TabIndex = 5;
            this.LBSubTotal.Text = "Sub Total:";
            // 
            // TBTaxAmount
            // 
            this.TBTaxAmount.Location = new System.Drawing.Point(71, 178);
            this.TBTaxAmount.Name = "TBTaxAmount";
            this.TBTaxAmount.Size = new System.Drawing.Size(119, 20);
            this.TBTaxAmount.TabIndex = 8;
            // 
            // LBTaxAmount
            // 
            this.LBTaxAmount.AutoSize = true;
            this.LBTaxAmount.ForeColor = System.Drawing.Color.MidnightBlue;
            this.LBTaxAmount.Location = new System.Drawing.Point(3, 181);
            this.LBTaxAmount.Name = "LBTaxAmount";
            this.LBTaxAmount.Size = new System.Drawing.Size(67, 13);
            this.LBTaxAmount.TabIndex = 7;
            this.LBTaxAmount.Text = "Tax Amount:";
            // 
            // TBTotalDue
            // 
            this.TBTotalDue.Enabled = false;
            this.TBTotalDue.Location = new System.Drawing.Point(72, 204);
            this.TBTotalDue.Name = "TBTotalDue";
            this.TBTotalDue.Size = new System.Drawing.Size(119, 20);
            this.TBTotalDue.TabIndex = 10;
            // 
            // LBTotalDue
            // 
            this.LBTotalDue.AutoSize = true;
            this.LBTotalDue.ForeColor = System.Drawing.Color.MidnightBlue;
            this.LBTotalDue.Location = new System.Drawing.Point(4, 207);
            this.LBTotalDue.Name = "LBTotalDue";
            this.LBTotalDue.Size = new System.Drawing.Size(57, 13);
            this.LBTotalDue.TabIndex = 9;
            this.LBTotalDue.Text = "Total Due:";
            // 
            // BTUpdateOrder
            // 
            this.BTUpdateOrder.ForeColor = System.Drawing.Color.MidnightBlue;
            this.BTUpdateOrder.Location = new System.Drawing.Point(86, 241);
            this.BTUpdateOrder.Name = "BTUpdateOrder";
            this.BTUpdateOrder.Size = new System.Drawing.Size(104, 23);
            this.BTUpdateOrder.TabIndex = 11;
            this.BTUpdateOrder.Text = "Update Order";
            this.BTUpdateOrder.UseVisualStyleBackColor = true;
            this.BTUpdateOrder.Click += new System.EventHandler(this.BTUpdateOrder_Click);
            // 
            // LBRelatedOrderList
            // 
            this.LBRelatedOrderList.AutoSize = true;
            this.LBRelatedOrderList.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.LBRelatedOrderList.ForeColor = System.Drawing.Color.DarkRed;
            this.LBRelatedOrderList.Location = new System.Drawing.Point(4, 13);
            this.LBRelatedOrderList.Name = "LBRelatedOrderList";
            this.LBRelatedOrderList.Size = new System.Drawing.Size(106, 15);
            this.LBRelatedOrderList.TabIndex = 12;
            this.LBRelatedOrderList.Text = "Related Order List";
            // 
            // CBRelatedOrederList
            // 
            this.CBRelatedOrederList.FormattingEnabled = true;
            this.CBRelatedOrederList.Location = new System.Drawing.Point(7, 35);
            this.CBRelatedOrederList.Name = "CBRelatedOrederList";
            this.CBRelatedOrederList.Size = new System.Drawing.Size(184, 21);
            this.CBRelatedOrederList.TabIndex = 13;
            this.CBRelatedOrederList.SelectedIndexChanged += new System.EventHandler(this.CBRelatedOrederList_SelectedIndexChanged);
            // 
            // CustomOBPart
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.CBRelatedOrederList);
            this.Controls.Add(this.LBRelatedOrderList);
            this.Controls.Add(this.BTUpdateOrder);
            this.Controls.Add(this.TBTotalDue);
            this.Controls.Add(this.LBTotalDue);
            this.Controls.Add(this.TBTaxAmount);
            this.Controls.Add(this.LBTaxAmount);
            this.Controls.Add(this.TBSubtotal);
            this.Controls.Add(this.LBSubTotal);
            this.Controls.Add(this.DTOrderDate);
            this.Controls.Add(this.LBOrderDate);
            this.Controls.Add(this.LBRelatedOrderTitle);
            this.Controls.Add(this.TBOrderID);
            this.Controls.Add(this.LBOrderID);
            this.Name = "CustomOBPart";
            this.Size = new System.Drawing.Size(212, 287);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label LBOrderID;
        private System.Windows.Forms.TextBox TBOrderID;
        private System.Windows.Forms.Label LBRelatedOrderTitle;
        private System.Windows.Forms.Label LBOrderDate;
        private System.Windows.Forms.DateTimePicker DTOrderDate;
        private System.Windows.Forms.TextBox TBSubtotal;
        private System.Windows.Forms.Label LBSubTotal;
        private System.Windows.Forms.TextBox TBTaxAmount;
        private System.Windows.Forms.Label LBTaxAmount;
        private System.Windows.Forms.TextBox TBTotalDue;
        private System.Windows.Forms.Label LBTotalDue;
        private System.Windows.Forms.Button BTUpdateOrder;
        private System.Windows.Forms.Label LBRelatedOrderList;
        private System.Windows.Forms.ComboBox CBRelatedOrederList;
    }
}

See Also

Reference

IEntityInstance

IEntityInstanceEnumerator

Entity

GetSourceAssociations()

IAssociation

IFilterCollection

ILobSystem

ILobSystemInstance

EntityInstanceCollection

GetFilters()

GetDestination()

GetLobSystem()

GetLobSystemInstances()

FindAssociated(EntityInstanceCollection, IAssociation, ILobSystemInstance)

RemoteOfflineRuntime

GetSubscriptionManager()

ISubscriptionManager

GetSubscription(String, String, String, String)

ISubscription

RequestRefresh(Boolean)