Another way of sending Data from External List to Data View Web Part of Form page of a List

Previously, I told about how you can send Filtered data from an external list to a New/Edit form page’s Data View Web Part by connecting the web parts. Today, I will show another easy way to populate Data within a Data View Web Part sent from a External List.

This time around, I have used Inline Coding inside the Form Page (You have to go in Advanced mode to write inline code and modify the web.config for allowing).

Open up your form page and find the following:

<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">

I have to use my code inside this tag. So, first of all create some Text Fields which will grab the values of the Data sent from the External list.

<asp:TextBox ID="txtID" runat="server" Text="" Visible="false"/>
<asp:TextBox ID="txtFullName" runat="server" Text="" Visible="false"/>
<asp:TextBox ID="txtEmail" runat="server" Text="" Visible="false"/>
<asp:TextBox ID="txtPhone" runat="server" Text="" Visible="false"/>

Note that, I set the visibility to false as obviously you don’t want to show these on your Forms

For writing C# code inside the form, we have to add a tag like the following:

<script runat="server" type="text/c#">

Now, I need to write the logic inside the Page_Load event as that is when my External List will interact with this form and populate the fields.

protected void Page_Load(object sender, EventArgs e)
        {
         
       //Get the current site
         SPWeb currentWeb = SPContext.Current.Web;
       //Get the current user
         SPUser currentUser = currentWeb.CurrentUser;
        //Get the current user's login name
              string username1 = currentWeb.CurrentUser.LoginName.ToString();
              
      //As current username's format is ABC\username, i need to use substring to get "username"
             string username2 = username1.Remove(0,4); 

      //This username exist in the external List as Upper Case
             string currentUsername=username2.ToUpper(); 
         
      //Elevate the permission of current user to use System Account
         SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                
                    using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                        
                        //Get the List of this current form page, in my case it is "Request For Change"
                         SPList list1 = web.Lists["Request For Change"];  
                            
                            bool isMember=false;
                          //Checking the current user whether he belongs to a particular group
                            isMember=SPContext.Current.Web.IsCurrentUserMemberOfGroup(web.Groups["Request For Change Members"].ID);
                            bool isModerator=false;
                            isModerator=SPContext.Current.Web.IsCurrentUserMemberOfGroup(web.Groups["Request For Change Moderators"].ID);
                            bool isAdmin=false;
                            isAdmin=SPContext.Current.Web.IsCurrentUserMemberOfGroup(web.Groups["Request For Change Admin"].ID);

                       //If the user does not belong to any group, redirect him to the main home page of the site
                            if(isMember==false && isModerator==false && isAdmin==false)
                            {
                                Context.Response.Redirect(SPContext.Current.Web.Url.ToString());
                            }

                  //Get the External List, in my case the name is "eBusinessInfo"      
              SPList list=web.Lists["eBusinessInfo"];
                 //Using CAML
               SPQuery theQuery=new SPQuery();

                            theQuery.ViewFields="<FieldRef Name='ID' /><FieldRef Name='FULL_NAME' /><FieldRef Name='EMAIL' /><FieldRef Name='WORK_PHONE' /><FieldRef Name='USER_NAME' />" ;
                            theQuery.Query = @"<Where>
                            <Eq>
                                <FieldRef Name='USER_NAME' />
                                <Value Type='Text'>"+ currentUsername +@"</Value>
                            </Eq>
                            </Where>";
                            //get the Result of CAML query
                            SPListItemCollection foundItem = list.GetItems(theQuery);
                           //Store the ID, Full Name, Email and Phone inside the TextBox that we created before
                            txtID.Text= foundItem[0]["ID"].ToString();
                            txtFullName.Text = foundItem[0]["FULL_NAME"].ToString();
                            string strEmail= foundItem[0]["EMAIL"].ToString();
                            if(foundItem[0]["WORK_PHONE"]==null)
                            txtPhone.Text= "";
                            else
                            txtPhone.Text=foundItem[0]["WORK_PHONE"].ToString();
                            //string strPhone=foundItem[0]["WORK_PHONE"].ToString();
                            if(strEmail != null)
                            
                            txtEmail.Text = foundItem[0]["EMAIL"].ToString();
                            else
                            txtEmail.Text= "N/A";
                            
                        
                            }
                        }
                    
                    });

        }

</script>

Upto now, I have retrieved the value from the external list using a CAML query and stored the value inside the TextBox Fields. Now, In order to use these values inside my form, I have to find the following web part zone:

<WebPartPages:WebPartZone runat=“server” FrameType=“None” ID=“Main” Title=“loc:Main”><ZoneTemplate>

 
add the parameters inside this web part zone:
<ParameterBinding Name="paramID" Location="Control(txtID, Text)" DefaultValue=""/> 
<ParameterBinding Name="paramFullName" Location="Control(txtFullName, Text)" DefaultValue=""/>
<ParameterBinding Name="paramEmail" Location="Control(txtEmail, Text)" DefaultValue=""/>
<ParameterBinding Name="paramPhone" Location="Control(txtPhone, Text)" DefaultValue=""/>

 

Then, add the parameters inside the XSLT right after this web part zone.

<xsl:param name="paramID" />
<xsl:param name="paramFullName" />
<xsl:param name="paramEmail" />
<xsl:param name="paramPhone" />

Now, Instead of using SharePoint:FormField for the desired fields, change those like the following:

<asp:TextBox runat="server" id="ff1{$Pos}" ReadOnly="True" Text="{$paramID}" __designer:bind="{ddwrt:DataBind('i',concat('ff1',$Pos),'Text','TextChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@ID')}"/>

While changing this, do not forget to change “Value” and “ValueChanged” to “Text” and “TextChanged” respectively inside the Tag. That’s it. Now, save the form and see the result!

Advertisement

How to create an External List from an External Content Type after deploying your Business Data Connectivity Model?

When you deploy a BDC Model in SharePoint 2010 as an External Content Type, it gets stored in your web application. But, your site and site collection cannot just use that yet because of some permission issues. So, in order to let that External Content type usable in your sites and site collections, you have to go to the central administration.

image

Go to Application management->Manage Service Applications. Click on Business Data Connectivity Service.

image

You will see your external content type here if you have successfully deployed your BDC model using any connector (SQL server, .NET Assembly or WCF Service).

image

Select your External Content Type and Click on “Set Object Permissions”. After that, you will see the following window where you will need to add your users who can use this content type in your sites and site collections.

image

Now that you are done with the permission settings, you will be able to create External List from this External content type inside your site.

Go to your site and from the “Site Actions”, go to more options and and click on List like the following screenshot:

image

From here, Choose “External List” and click on Create, you will get the following window:

image

Type in a Name for your External List and Choose the External Content Type from the Item Picker (Furthest icon to the right under “External Contet Type” box). You will see your External Content Type there ( You can also write the name of your ECT directly here)

image

After selecting the content type, the name of your “External Content Type” will be displayed. Click on “Create”.

That’s it. After that, you will be redirected to the Default View of your newly created External List.