Tuesday, June 28, 2016

Web Part That Can Read and Write Data to an External Data Source Using BCS sharepoint 2013

CODE behind:

using System;
using System.Drawing;
using System.Web.UI;
using Microsoft.SharePoint.BusinessData.SharedService;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

using Microsoft.BusinessData.Infrastructure;
using Microsoft.BusinessData.MetadataModel.Collections;

namespace spvwp.CommanderWebPart
{
    public partial class CommanderWebPartUserControl : UserControl
    {
        #region Properties

        //protected string EntityNamespace
        //{
        //    get { return ECTNamespace.Text.Trim(); }
        //}

        //protected string EntityName
        //{
        //    get { return ECTName.Text.Trim(); }
        //}

        //protected bool EntityValuesAreSet
        //{
        //    get
        //    {
        //        if (EntityNamespace == string.Empty ||
        //          EntityName == string.Empty)
        //            return false;
        //        else
        //            return true;
        //    }
        //}

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btncreate_Click(object sender, EventArgs e)
        {
            try
          {

            using (new Microsoft.SharePoint.SPServiceContextScope(SPServiceContext.GetContext(SPContext.Current.Site)))
            {
                // Get the BDC service and metadata catalog.
                BdcService service = SPFarm.Local.Services.GetValue<BdcService>();
                IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(SPServiceContext.Current);

                // Get the entity by using the specified name and namespace.
                IEntity entity = catalog.GetEntity("<entity namespace>", "<entity name>");
                ILobSystemInstance LobSystemInstance = entity.GetLobSystem().GetLobSystemInstances()[0].Value;

                IView createview = entity.GetCreatorView("Create");
                IFieldValueDictionary valueDictionary = createview.GetDefaultValues();

                // Set the values of the entity fields.

                valueDictionary["CommanderName"] = txtCommanderName.Text;


                // Call the creator method and display the returned
                // Customer ID.
                Identity id = entity.Create(valueDictionary, LobSystemInstance);

                CommanderID.Text = id.GetIdentifierValues().GetValue(0).ToString();

                StatusLabel.ForeColor = Color.Green;
                StatusLabel.Text = "Commander added successfully With ID: " + CommanderID.Text;

            }
          }
            catch (Exception ex)
            {
                StatusLabel.ForeColor = Color.Red;
                StatusLabel.Text = "Unable to create Commander." +
                  ex.Message;
            }

        }
    }
}


Webpart After Deployed to SharePoint 2013: