SharePoint Developer Center > SharePoint Products and Technologies Forums > SharePoint - Development and Programming > SPGridView mapping Internal Names vs Display Names, and read only fields
Ask a questionAsk a question
 

QuestionSPGridView mapping Internal Names vs Display Names, and read only fields

  • Tuesday, November 03, 2009 7:42 AMJeff Law - JSL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I have another question relating to SPGridView and mapping SharePoint list data.

    I have been doing some testing with a simple sample list that contains my 6 extra fields. Got it all working well. Now I have migrated this across to a test platform, and I am starting to have some problems, which I think could have one of two causes.

    The error I am getting on the new testing platform, when I load the list I want to view is:

    getListData: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'RMExpiryDate'.

     (where getListData is the location in my webpart where the execption is being thown and the rest of the line is the exception message.)

    The two possibilities I can think of are:
    1) The definition in the aspx file is using the Internal Name, rather than the Display Name of the list field. In my initial tests, these were all the same, but now, I have to allow for the fact that the display name can be different from the Internal name - which it is on the Test environment.
    2) The fields in the new test platform have been defined as READ ONLY - Some of them Hidden. Could this make a difference.

    The relevant portion of my aspx file looks like: (This is inside the <Columns> tag along with the other fields. I use this one as it is the one being complained about at the moment.


    <asp:TemplateField HeaderText="Expiry Date" >
    
    <ItemTemplate><asp:Label runat="server" ID="RMExpiryDate"  Text='<%#Bind ("RMExpiryDate", "{0:d}") %>' /></ItemTemplate>
    
    <EditItemTemplate><asp:TextBox runat="server" ID="txtExpiryDate" Text='<%#Bind ("RMExpiryDate", "{0:d}") %>' /></EditItemTemplate>
    
    </asp:TemplateField>
    
    

    The following is the bit of code that attempts to fill up the gridview.
            private void getListData()
            {
                this.grdItems.DataSource = fillDataTable(new Guid(ddlSitesList.SelectedValue), new Guid(ddlWebsList.SelectedValue), new Guid(ddlListsList.SelectedValue));
                try
                {
                    this.grdItems.DataKeyNames = new string[] { "ID" };
                    this.grdItems.DataBind();
                    this.grdItems.Visible = true;
                }
    
                catch (Exception ex)
                {
                    tbMessage.Visible = true;
                    tbMessage.Text = "getListData: " + ex.Message;
                }
    
            }
    
            private DataTable fillDataTable(Guid theSiteGuid, Guid theWebGuid, Guid theListGuid)
            {
                DataTable dt = new DataTable();
                try
                {
                    using (SPSite theSite = new SPSite(theSiteGuid))
                    {
                        using (SPWeb theWeb = theSite.OpenWeb(theWebGuid))
                        {
                            SPList theList = theWeb.Lists[theListGuid];
                            dt = theList.Items.GetDataTable();
                        }
                    }
                }
    
                catch (Exception ex)
                {
                    tbMessage.Visible = true;
                    tbMessage.Text = "fillDataTable: " + ex.Message;
                }
    
                return dt;
            }
    
    


    In the Test environment, the field now has a display name of "RM Expiry Date"in SharePoint.

    As I say, this all works well on my dev environment with the same webpart, but my list manually created.

    Anyone got any experiance with this sort of thing?

    Regards
    Jeff

All Replies

  • Tuesday, November 03, 2009 9:38 AMSohel Rana Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    You should use internal name always as this is used internally. When you create the column 'RM Expiry Date', its internal name should be 'RM_x0020_Expiry_x0020_Date'. So when you'll get the data table the column name will be this one. You'll get the display name of the column in DataColumn's caption property. So answers are:

    1. You should use internal name always. Display name will work if you have no special character in your display name like space (in this case display name and internal will be the same). The data binding to column RMExpiryDate should not work at all when your column name is 'RM Expiry Date' except once scenario. The scenario is that you had created the column with 'RMExpiryDate' and then later renamed it 'RM Expiry Date'.

    2. Read only should not have a problem with this as long as you don't want to update the filed.





    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com
  • Tuesday, November 03, 2009 6:22 PMJeff Law - JSL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for your reply.

    I always create the fields without spaces, then rename them. I dont like all the special characters introduced to replace those spaces. :-)

    I do want to update these fields, but I was hoping that one of two situations could be used. I would either use elevated privileges, or use systemupdate.

    This particular webpart is only used by one person, and is not for general release.

    Regards
    Jeff
  • Tuesday, November 03, 2009 10:56 PMRiza Ture Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Sohel,
    You can use http://www.codeplex.com/SPCamlViewer, so easily you can see what SharePoint used as internal name for this field, also <ItemTemplate><asp:Label runat="server" ID="RMExpiryDate" change to <ItemTemplate><asp:Label runat="server" ID="lblRMExpiryDate"
  • Wednesday, November 04, 2009 11:07 PMJeff Law - JSL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi Riza

    Thanks for your suggestions.

    I have altered my asp:Labels so that the ID's are prefixed by lbl.

    I already had the U2U CAML builder installed, and used that. I noticed that the fields didnt appear in the list that it displayed at all.


    I also used the SharePoint Manager 2007 utility to view the fields and I noticed that of the 6 fields that I am trying to view/edit, all 6 are marked as Read Only, and 4 f them are defined as Hidden. Guess what?  RMExpiryDate is on of the ones that is marked as Hidden.

    So I am guessing that the Hidden is the problem.

    I have also tried adding the following SPQuery thinking that by default, these fields wont get retrieved, but perhaps if I specify the ViewFields, this will override the Hidden and retreive them, but I have not had any luck yet.

    Even tried using RunWithElevatedPrivileges but still no joy.

            private DataTable fillDataTable(Guid theSiteGuid, Guid theWebGuid, Guid theListGuid)
            {
                DataTable dt = new DataTable();
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite theSite = new SPSite(theSiteGuid))
                        {
                            using (SPWeb theWeb = theSite.OpenWeb(theWebGuid))
                            {
                                SPList theList = theWeb.Lists[theListGuid];
                                SPQuery qry = new SPQuery();
                                qry.Query = "<OrderBy><FieldRef Name='ID' Ascending='True' /></OrderBy>";
                                qry.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='Title' /><FieldRef Name='RMRecordID' /><FieldRef Name='RMRecordStatus' /><FieldRef Name='RMExpiryDate' /><FieldRef Name='RMOverrideDate' /><FieldRef Name='RMLastViewedDate' /><FieldRef Name='RMLastViewedBy' />";
                                dt = theList.GetItems(qry).GetDataTable();
                            }
                        }
                    });
    
                }<br/>
    
    So the question is now: How do you access Hidden fields? I presume you can do it somehow, otherwise they would be pretty useless...

    Regards
    Jeff