Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

i have defined some site columns which were referenced by some content types. These are used for a ListDefinition and a ListInstance. (Everything is in one Visual Studio Project). Now i would like to embed this list instance to an aplication page, beeing able to show that list and some other stuff at the same page. I definetly want to show the correct SharePoint list and not building (for example) a table.

In some other tests i was able to successfully use "WebPartPages:XsltListViewWebPart..." but in that test i created the list by the SharePoint webpage and not through visual studio. The problem with "WebPartPages:XsltListViewWebPart" is, that i need to enter the correct ListId. I added the ID attribute in the Elements.xml of the ListInstance to avoid a dynamically built id but i always get the error message, that the list was not found (probably removed). I checked the list id of my list (in the address bar of my browser when i click edit list) and found out, that the list id changes every time i build/deploy my project although the id attribute of my list instance definition.

Can please somebody help me, correctly embedding my list?

share|improve this question
    
Perhaps someone can explain me, why a list instance always get another id although i set the id attribute. If i could force it to keep the same id every time i build/deploy, my above problem would be solved. –  Chris Sep 26 '12 at 13:14

2 Answers 2

If you know the title of your list you could try something like this (here from Feature Event Receiver):

var web = properties.Feature.Parent as SPWeb;
if (web == null) return;
var list = web.Lists["MyList"];
var manager = web.GetLimitedWebPartManager("MyPage.aspx", PersonalizationScope.Shared);
var xsltListViewWebPart = new XsltListViewWebPart { ListId = list.ID };
manager.AddWebPart(xsltListViewWebPart , "MyZone", manager.WebParts.Count + 1);
share|improve this answer
    
thank you for you answer. My problem is, that when i add this code in my Page_Load EventHandler the page (in your example "MyPage.aspx") will not be found on execution. Perhaps the page isn't built at this time? –  Chris Sep 25 '12 at 14:35
    
I was thinking about running this in an Feature Event Receiver, so it will only be run once; when your Feature is activated (and the application page is deployed through some module). –  eirikb Sep 25 '12 at 14:37
    
Page_Load is part of when the page is accessed, as in opened normally in a browser. You can read about the Page Life Cycle on msdn. –  eirikb Sep 25 '12 at 14:39
    
When you say Application Page, do you really mean a Site Page deployed through a module, or a real Application Page, deployed to layouts? –  Derek Gusoff Sep 25 '12 at 14:59
    
if you mean application page read Andrey's response here: sharepoint.stackexchange.com/questions/26508/… –  Derek Gusoff Sep 25 '12 at 15:00

When your list is created, the instance of this list is saved to the content data base, in this time the data base assign a new ID for your list to be sure that this ID is unique.

I think that you can use ListUrl property of XsltListViewWebPart instead of list ID in web part definition. It is unique value for each web.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.