Domanda

I am trying to convert a SharePoint SPListItem into a ListItem to put into a Drop Down List. My problem is that the data in the SharePoint list is stored like this:

;#Daylight;#

;#Design;#Employee Engagement;#

But Obviously this does not work for me. I need to remove the ;# symbols which should be as simple as String.Replace but where I am running into some problems is what should I do for list items that contain multiple selections (my second example above). The ultimate goal here is to generate a drop down list (that does not contain duplicates) of all the items in the SharePoint List. Any ideas?

        using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
        using (SPWeb oWebsiteRoot = site.OpenWeb())
        {
            SPList oList = oWebsiteRoot.Lists["WplData"];
            SPListItemCollection items = null;
            SPQuery query = new SPQuery();
            query.Query = "<Where><IsNotNull><FieldRef Name='Topic' /></IsNotNull>" +
                "</Where><OrderBy><FieldRef Name='Topic' Ascending='True' /></OrderBy>";
            items = oList.GetItems(query);
            DataTable tmpTable = new System.Data.DataTable();
            tmpTable = items.GetDataTable();
            DataView view = new DataView(tmpTable);
            String[] columns = { "Topic" };
            DataTable table = view.ToTable(true, columns);
            foreach (DataRow row in table.Rows)
            {
                foreach (var item in row.ItemArray)
                {
                    ListItem listItem = new ListItem();
                    listItem.Value = item.ToString();
                    listItem.Text = item.ToString();
                    TopicDropDownList.Items.Add(listItem);
                }
            }
       }
È stato utile?

Soluzione

Instead of a DataTable, try SPFieldLookupValueCollection:

SPList oList = oWebsiteRoot.Lists["WplData"];
SPListItemCollection items = null;
SPQuery query = new SPQuery();
query.Query = "<Where><IsNotNull><FieldRef Name='Topic' /></IsNotNull>" +
    "</Where><OrderBy><FieldRef Name='Topic' Ascending='True' /></OrderBy>";
items = oList.GetItems(query);
foreach (SPListItem item in items)
{
    SPFieldLookupValueCollection values = 
        new SPFieldLookupValueCollection(item["Topic"].ToString());
    foreach (SPFieldLookupValue value in values)
    {
        ListItem listItem = new ListItem();
        listItem.Value = value.LookupId.ToString();
        listItem.Text = value.LookupValue;
        TopicDropDownList.Items.Add(listItem);
    }
}

Note, since you specified SharePoint 2007, I am avoiding the use of LINQ because that requires the .NET Framework 3.5. Also, as long as each list item contains a distinct set of topics, you will be fine. But if topics can be repeated among list items, you will want to modify this code so that TopicDropDownList contains a distinct set of topics.

Altri suggerimenti

LINQ it, something like (psuedocode)

...
var ddlItems = (from i in items
             select i["Topic"]).Distinct();
TopicDropDownList.Items.AddRange(ddlItems);
...

If Topic is a multi-choice field you can split the field value by ;#:

var listItems = items.Cast<SPListItem>()
  .SelectMany(i => Convert.ToString(i["Topic"]).Split(";#", StringSplitOptions.RemoveEmptyEntries))
  .Distinct();

TopicDropDownList.Items.AddRange(listItems);

You can directly operate on the SPListItemCollection returned by oList.GetItems(query). You do not need to convert it in a DataTable.

You might considering including the Topic field as ViewField in your query.

If you don't want to split the field value manually, you could use the SPFieldMultiChoiceValue class. Unfortunately there is no handy way to access the choices in this class. There is only a count and an indexer so you have to use a for-loop.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top