Code Project

Link Unit

Monday, September 07, 2009

DataBinding: 'Microsoft.SharePoint.SPListItem' does not contain a property with the name

When trying to bind the 'Extension' column of a custom Sharepoint List to the DropdownList , I was facing the error :

DataBinding: 'Microsoft.SharePoint.SPListItem' does not contain a property with the name 'Extension'

In case I tried the following it ran successfully

using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Departments"];
cboDepartment.DataSource = list.Items;
cboDepartment.DataValueField = "Title"; // List field holding value
cboDepartment.DataTextField = "Title"; // List field holding name to be displayed on page
cboDepartment.DataBind();
}

So, I believe that whatever it is tryed to bind through DataTextField and DataValueField is supposed to be a property of the object we are binding to. For instance, SPListItem do have Title and ID properties, but it does not have 'Extension' property (in the meaning of the SPListItem class member).

So the following solutions were tried.

Solution #1

SPWeb site = SPContext.Current.Web;
DropDownList cboExtensions = new DropDownList();
SPList list = site.Lists["ImageExtensionList"];
SPListItemCollection lstCollection = list.Items;
cboExtensions.DataSource = lstCollection.GetDataTable();
cboExtensions.DataValueField = "Extension"; // List field holding value
cboExtensions.DataTextField = "Extension"; // List field holding name to be displayed on page
cboExtensions.DataBind();
this.Controls.Add(cboExtensions);


Solution #2

SPWeb site = SPContext.Current.Web;
DropDownList cboExtensions = new DropDownList();
SPList list = site.Lists["ImageExtensionList"];
foreach (SPListItem listRecords in list.Items)
{
//ListItem tempItem = listRecords["Extension"].ToString();
ListItem tempItem = new ListItem(listRecords["Extension"].ToString(), listRecords["Extension"].ToString());
cboExtensions.Items.Add(tempItem); //ddlFromSPList is the name of the dropdown list
}

Conclusion:
Whenever Title or ID is specified it will work without any issue. When the ListItemCollection is bounded to the DropDownList ,it is expecting Valuefield and TextField to be Properties. "Title" and "ID" are properties of List , hence it worked without any issue.

So the simple solution is to get the DataTable from the ListCollection or Get the items of the list , iterate the list and add the items to DropDownlist.

Hope it Helps
Jatinder Singh

Friday, September 04, 2009

This solution contains two assemblies with the same name, or the SharePoint server already has an assembly with the specified name.

While trying to redeploy the same WebPart ,I was facing this rather silly error. I tried to do the “cleanup” by right clicking the Project and then redeployed same error appeared. Later figured it out by throwing a search in Google



#1 See if the feature that you're trying to add still exists in the "\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES" path. Delete it.


#2 Uninstall the particular Assembly from c:\windows\assembly folder too.


#3 Try to deploy it again.

Design decision to use ascx control in sharepoint webpart versus pure sharepoint webpart


1. Create asp.net webusercontrol (.ascx), and then load that user control in the newly created webpart.

2. Create a webpart from scratch, by putting the code to generate each and every control in code.

if you need to create a control with a really advanced user interface, for example many controls and stuff, then a user control could be much easier to create.

I have found that the execution lifecycle (control viewstate, event bubbling, etc.) is a pain when going with Option 2. I choose Option 1 because the viewstate is easily managed in the code. Additionally, I like Option 1 because I can visually design the page using the VS toolbox to drag and drop controls on the design surface as opposed to Option 2 where you have to handle each control manually in code.

The approach 2 may sound difficult because of not very great integration with Galleries , but if you have the luxury of invest the time, you'll be better off in the long run.

The type or namespace name 'Data' does not exist in the namespace 'System' (are you missing an assembly reference?)

This is a common but simple problem to solve , below is explanation and steps to solve.

using Directives at the top of code files just allows you to use types in a namespace without having to fully qualify them.
e.g. using System.Data directive allows you to declare a DataColumn as following:

DataColumn dc = new DataColumn();

instead of:

System.Data.DataColumn dc = new System.Data.DataColumn();

using statements define scope, but you still need a reference to the Library so that the project will build.

Do you see System.Data listed under it?
Open your solution explorer and Expand References:
More than likely not, hence the complaint from the Compiler.

What to do/ how to Solve??

Right click on References, and from the Context Menu, select "Add Reference"
The Add Reference view will default to the .NET tab, scroll down until you find the System.Data, select it and click the OK button.

Build your app.

Hope it Helps

Wednesday, September 02, 2009

The feature name WebPart1 already exists in SharePoint. You need to rename the feature before solution deployment can succeed

While working in Visual Studio 2008 , we were able to create new WebParts and deploy them easily . Off late we started facing the following problem.

“The feature name WebPart1 already exists in SharePoint. You need to rename the feature before solution deployment can succeed.”

The error may seem stupid , the solution to it is simple.

A simple method to avoid the above error is as follows
  1. Immediately after creating a new webpart project, remove the Webpart1 folder completely.

  2. Add new web part to the project by right clicking on the project and selecting new item from the context menu.

  3. In the Add new item dialog box select Sharepoint from categories and Select Web Part from templates.

  4. Give a name for your webpart and click Add button.