views:

419

answers:

2

Is there a way to dynamically set the ListId field in the ListView control. We cannot guarantee that the list we are interested in has a consistent GUID between installations (The list is deployed as part of a site template that we do not control). I've tried using the PreInit event to set a variable (see the list guid: section. If I remove the ListView tag, I see the proper GUID printed out. So I'm collecting the GUID correctly. However, The listview control errors out with the following message "Guid should contain 32 digits with 4 dashes". This tells me that the tag is not getting the variable set. Is this correct? Is there another way to specify the list to use?

Can this be done?

Sample code follows:

<%@ Register TagPrefix="Sharepoint" ...details deleted.. %>
<br>
...stuff deleted.
<br>
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server"> 
  ... more stuff deleted...
  <p>list guid: <%=ListGuid %>    
  <Sharepoint:ListView ListId="<%=ListGuid %>" Enabled="true" runat="server" />
  <p>
</asp:Content>



<script runat="server">
string ListGuid = string.Empty;

protected void Page_PreInit(object sender, EventArgs e)
{

  SPSite Site = SPContext.Current.Site;
  using (SPWeb HelpDesk = Site.OpenWeb("HelpDesk"))
  {
    SPList list = HelpDesk.Lists["Charge Numbers"];
    ListGuid = list.ID.ToString();
  }

}

</script>
A: 

I think the error message you are receiving is telling you what's going wrong... the ToString() is converting the GUID to an invalid format. There is an overload for the tostring

list.ID.ToString("N");
list.ID.ToString("D"); (this is the default)
list.ID.ToString("B");
list.ID.ToString("P");

... Try them all

UJ
The "D" format is what it is asking for (at least according to the logs). I tried all four variants without success.
Russ
A: 

Having server-side code like you do that gets injected into more server side code is a little weird and seems unlikely to work. What does the SharePoint log say about the list ID that it's trying to load? My guess is that it's not really trying to load the list represented by the GUID that you're setting in your ListGuid variable.

Instead of trying to force some global ListGuid variable to have the correct guid, why not just set the control's ListId property inside your application page's Load event handler?

In the aspx file:

<SharePoint:ListView Id="lv" runat="server" />

In the application page's codebehind:

protected ListView lv

In the Load event:

SPList list = HelpDesk.Lists["Charge Numbers"];
Guid myguid = list.ID;
lv.ListId = myguid.ToString();
Chris Farmer
I had to remove the declaration of lv to avoid compiler errors. Thanks for the direction, I got it working once I removed that. Thanks.
Russ