Programmatic Drop Down Repeating Section

M

Mackenzie, Jason

I have a series of drop downs on my form that I populate programatically
with the results of the SharePoint People Web Service.

Everything works fine except for the drop down in a repeating section. The
idea is to add new team members and their can be more than one. I have the
drop down bound to a datasource that is an external XML file. The
datasource is called Teammembers.

So the form opens and in the section there is a textbox and a button and a
drop down. The user enters a last name clicks the button and the drop down
is populated with anyone that matches the search text. The problem is that
when I insert a new item in the form, the new dropdown is still bound to the
same datasource so when I rebuild the list it rebuilds all the list boxes in
that repeating section. This makes sense as I'm rebuilding based on
datasource and they would all be bound to the same one.

Hopefully this makes some sense but I'm happy to provide any clarification
required.

Thanks,

Jason

The code is as follows:

public void btnTeamLookup_Clicked(object sender, ClickedEventArgs e)
{
LoadUserProfile("/my:myFields/my:group3/my:grpTeam/my:txtTeamLastName",
TeamMembers_DS);
}

private void LoadUserProfile(string LastName, string DataSetName)
{
XPathNavigator nav = MainDataSource.CreateNavigator();
//string firstName = nav.SelectSingleNode(prefix + "firstName",
NamespaceManager).Value;
string lastName = nav.SelectSingleNode(LastName,
NamespaceManager).Value;

// People finder
PeopleService.People svc = new PeopleService.People();
svc.UseDefaultCredentials = true;
StringBuilder srchName = new StringBuilder()
.Append(lastName);

// Call the web service - max 20 results
PeopleService.PrincipalInfo[] people =
svc.SearchPrincipals(srchName.ToString(), 20,
PeopleService.SPPrincipalType.User);

if (people != null)
{
// Fill in the name listbox
List<string> names = new List<string>();
foreach (PeopleService.PrincipalInfo person in people)
{
StringBuilder name = new StringBuilder()
.Append(person.DisplayName).Append(" - ")
.Append(person.AccountName);
names.Add(name.ToString());
}
if (names.Count > 0)
InitializeListbox(DataSetName, names.ToArray());
}
}

/// <summary>
/// Initializes the listbox.
/// </summary>
/// <param name="datasource">The listbox.</param>
private void InitializeListbox(string datasource, string[] values)
{
RemoveAllItems(datasource);
foreach (string value in values)
InsertItem(datasource, value);

RemoveBlankItem(datasource);
}

/// <summary>
/// Inserts the item.
/// </summary>
/// <param name="datasource">The listbox.</param>
/// <param name="value">The value.</param>
private void InsertItem(string datasource, string value)
{
// Get the empty list associated with the dropdown list
XPathNavigator nav = DataSources[datasource].CreateNavigator();
XPathNavigator items = nav.SelectSingleNode("//items",
NamespaceManager);
XPathNavigator item = nav.SelectSingleNode("//items/item",
NamespaceManager);

// save the previous values
string oldVal = item.SelectSingleNode("displayname").Value;

// create and add a new node
XPathNavigator newItem = item.Clone();
newItem.SelectSingleNode("value").SetValue(value);
newItem.SelectSingleNode("displayname").SetValue(value);
items.AppendChild(newItem);

// reset the first item
item.SelectSingleNode("value").SetValue(oldVal);
item.SelectSingleNode("displayname").SetValue(oldVal);
}

/// <summary>
/// Removes the blank item.
/// </summary>
/// <param name="datasource">The listbox.</param>
private void RemoveBlankItem(string datasource)
{
XPathNavigator nav = DataSources[datasource].CreateNavigator();
XPathNavigator item = nav.SelectSingleNode("//items/item",
NamespaceManager);
string val = item.Value;
if (string.IsNullOrEmpty(val))
item.DeleteSelf();
}

/// <summary>
/// Removes all items.
/// </summary>
/// <param name="datasource">The datasource.</param>
private void RemoveAllItems(string datasource)
{
XPathNodeIterator items =
DataSources[datasource].CreateNavigator()
.Select("//item", NamespaceManager);
if (items.MoveNext())
{
XPathNavigator start = items.Current;
XPathNavigator last = start;
int count = items.Count - 1; // allow for default value
if (count >= 1)
{
// remove all but one item
IEnumerator iter = items.GetEnumerator();
while (--count > 0 && iter.MoveNext())
last = (XPathNavigator)iter.Current;
start.DeleteRange(last);

}
// reset last item
last = items.Current;
last.SelectSingleNode("//item/value").SetValue("");
last.SelectSingleNode("//item/displayname").SetValue("");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top