“An unexpected error has occurred” error message in SharePoint.


Normally within SharePoint if an error occurs we would be receiving quite an unhelpful error message i.e. “An unexpected error has occurred”. It is because by default the detailed error messages are turned off for security reasons.

 

Here are the steps for getting the full error messages

 

1)      Go to the MOSS site for which you would like to enable it.

Most likely to be found at c:inetpubwwwrootwssVirtualDirectories and a sub directory with your web application’s port number.

 

2)      Locate and open the web.config file for editing.

 

3)      Find out the following entry

<SafeMode MaxControls=200 CallStack=false DirectFileDependencies=10 TotalFileDependencies=50 AllowPageLevelTrace=false>

 

And make following changes to it

 

<SafeMode MaxControls=200 CallStack=true DirectFileDependencies=10 TotalFileDependencies=50 AllowPageLevelTrace=true>

 

4)      And

<customErrors mode=On />

To

<customErrors mode=Off />

 

5)      Save and close web.config.

 

 

That’s it …

Using SPGridView to bound to list data in SharePoint


These are the few points we need to remember while using SPGridView

With SPGridView we would inherit the same look and feel as the rest of SharePoint site because it makes use of the same CSS classes that the other grids in SharePoint use.

We need to set AutoGenerateColumns=false and explicitly bind the columns.

Create a new asp.net page

Put the following directive to use SPGridView

<%@ Register TagPrefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls”

Assembly=”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %

Declare the control

<SharePoint:SPGridView runat=”server” ID=”grdView”

  AutoGenerateColumns=”false” /> 

Declaring class level variable

public partial class _Default : System.Web.UI.Page

{

    // refer to your site collection

    SPSite mySite = new SPSite(@”http://d-1246:100&#8243;);

    // create class level spweb and splist object

    SPWeb myWeb;

    SPList myList;

Code for Page_Load EventHandler

protected void Page_Load(object sender, EventArgs e)

    {

        myWeb = mySite.OpenWeb();

        myList = myWeb.Lists[“ListName”];

        if (!Page.IsPostBack)

        {

            BindToGrid(myList, grdPropertyValues);

        }

    }

Code for BindToGrid method

private void BindToGrid(SPList myList, SPGridView gridView)

    {

        //grdView.Columns.Clear();

        // get all the listitem

        SPListItemCollection results = myList.Items;

        // create the datatable object

        DataTable table;

        table = new DataTable();

        table.Columns.Add(“Type”, typeof(string));

        table.Columns.Add(“Name”, typeof(string));

        table.Columns.Add(“Created”, typeof(string));    

        // Create rows for each splistitem

        DataRow row;       

        foreach (SPListItem result in results)

        {

            row = table.Rows.Add();

            row[“Type”] = result[“Type”].ToString();

            row[“Name”] = result[“Name”].ToString();

            row[“Created”] = result[“Created”].ToString();   

        }

        // create the bound fields

        SPBoundField boundField;

        boundField = new SPBoundField();

        boundField.HeaderText = “Type”;

        boundField.DataField = “Type”;

        boundField.ItemStyle.HorizontalAlign = HorizontalAlign.Center;

        boundField.ItemStyle.Wrap = false;

        gridView.Columns.Add(boundField);

 

 

        boundField = new SPBoundField();

        boundField.HeaderText = “Name”;

        boundField.DataField = “Name”;

        gridView.Columns.Add(boundField);

 

        boundField = new SPBoundField();

        boundField.HeaderText = “Created”;

        boundField.DataField = “Created”;

        gridView.Columns.Add(boundField);

       

        gridView.AutoGenerateColumns = false;

        gridView.DataSource = table.DefaultView;

        gridView.DataBind();  

    }

 

That’s it …

Adding a Read Only Field or Read Only Column to a SharePoint List


We had a requirement to add a read only column to one of our document library. Using Create column it isn’t possible to add a read only column.  One option could be to create the new column and then using SharePoint designer we can make use of JavaScript and modify the NewForm.aspx and EditForm.aspx to display that field as read only.

We thought of adding it as a Site Column making use of ReadOnly property of field.

<?xml version=1.0 encoding=utf-8?>

<Elements xmlns=http://schemas.microsoft.com/sharepoint/>

                <Field ID={0B8A5574-80BF-4d5e-99B9-9A25D8E8D21E}

                                   Name=_IsApproved

                                   DisplayName=Is Document Approved?

                                   Group=Custom Columns

                                   Type=Text                      

                                   Required=FALSE      

                                   ReadOnly=TRUE       

                                 

                                   >

                </Field>            

</Elements>

However if we set ReadOnly as True the field doesn’t appear on Site Settings pages for managaing site columns and content types. However we can add it to the view using the below code

 

SPSite oSiteCollection = new SPSite(@”http://servername:port&#8221;);

            using (SPWeb oWebsite = oSiteCollection.OpenWeb())

            {

                SPList oList = oWebsite.Lists[“ListName”];

                oList.Fields.Add(“Is Document Approved?”, SPFieldType.Text, false);

                oList.Update();

                SPView oView = oList.DefaultView;

                oView.ViewFields.Add(“Is Document Approved?”);             

                oView.Update();

                }

However the field was still appearing in editform.aspx and newform.aspx in editable mode.         

So finally tried this

Modified the definition for the custom site column as following

<?xml version=1.0 encoding=utf-8?>

<Elements xmlns=http://schemas.microsoft.com/sharepoint/>

                <Field ID={0B8A5574-80BF-4d5e-99B9-9A25D8E8D21E}

                                   Name=_IsApproved

                                   DisplayName=Is Document Approved?

                                   Group=Custom Columns

                                   Type=Text                      

                                   Required=FALSE      

                                   ReadOnly=FALSE

                                   ShowInDisplayForm=TRUE

                                   ShowInEditForm=FALSE

                                   ShowInNewForm=FALSE                        

                                   >

                </Field>            

</Elements>

 

Setting ShowInDisplayForm and ShowInEditForm as False and keeping ReadOnly as False so that the field could appear within Site Settings pages for managing site columns.

This last solution worked ..

The feature file for installing the above site column

<?xml version=1.0 encoding=utf-8?>

<Feature  Id=D829F71B-7FCC-4f0d-950D-6B562AFF400E

          Title=MyCustom Feature

          Description=This is my custom column feature

          Version=12.0.0.0

          Scope=Site

          xmlns=http://schemas.microsoft.com/sharepoint/>

                <ElementManifests>

                                <ElementManifest Location=MyCustomColumn.xml />                          

                </ElementManifests>  

</Feature>

 

Now the only way to edit the column was through SharePoint Object Model

   SPSite oSiteCollection = new SPSite(@”http://servername:port&#8221;);

            using (SPWeb oWebsite = oSiteCollection.OpenWeb())

            {

                SPList oList = oWebsite.Lists[“ListName”];        

                SPListItem item = oList.Items[0];         

                item[“Is Document Approved?”] = “These are my changes”;

                item.Update();              

            }

That’s it..