You need to open the site/web INSIDE of the elevated section or it won't be 
elevated.




Regards,

Paul Turner
MCT, MCPD: Enterprise, MCSD, MCDBA, MCTS, Microsoft vTSP
Consultant

M: 0412 748 168
A: 66 Henley Beach Road, Mile End SA 5031
E: [email protected]<mailto:[email protected]>  W: 
www.dws.com.au<http://www.dws.com.au/>

This email and any files transmitted with it are confidential and are only for 
the use of the person to whom they are addressed. If you are not the intended 
recipient you have received this email in error and are requested to delete it 
immediately. Any opinion expressed in this e-mail may not necessarily be that 
of DWS Pty Ltd.
Please consider the environment before printing this email.
________________________________
From: [email protected] [[email protected]] on behalf of Daniel 
Brown [[email protected]]
Sent: Monday, 7 February 2011 2:12 PM
To: ozMOSS
Subject: SP2010 XsltListViewWebPart

Howdy all,

A bit of a developer question here.... I've got a XsltListViewWebPart added on 
a SP2010 site page, in which I want to swap in and out which view it is using 
at will.

Just doing it though a console application which will do it for now (will be a 
feature activated event).

Code:


            using (SPSite site = new SPSite(siteurl))
            {
                using (SPWeb currentWeb = site.OpenWeb())
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        currentWeb.AllowUnsafeUpdates = true;

                        SPFile CurrentPage = currentWeb.GetFile(pageurl);

                        using (SPLimitedWebPartManager wpm = 
currentWeb.GetLimitedWebPartManager(pageurl, PersonalizationScope.Shared))
                        {
                            const string SourceListName = "Shared Documents";
                            const string SourceViewName = "Documents View";
                            const string DestViewName = "All Documents";

                            // get view ID
                            SPList SourceList = 
currentWeb.Lists[SourceListName];
                            SPView SourceListView = 
SourceList.Views[SourceViewName];
                            SPView DestListView = 
SourceList.Views[DestViewName];

                            // loop though page, get LVWP
                            var listViews = 
wpm.WebParts.Cast<System.Web.UI.WebControls.WebParts.WebPart>()
                                .Where(webpart => webpart is 
XsltListViewWebPart)
                                .Cast<XsltListViewWebPart>();

                            // get first LVWP
                            XsltListViewWebPart listViewWP = listViews.First();

                            listViewWP.ViewGuid = 
DestListView.ID.ToString("B").ToUpper();
                            listViewWP.Visible = true;
                            listViewWP.Title = "changed!";
                            wpm.SaveChanges(listViewWP);


                            currentWeb.AllowUnsafeUpdates = false;
                            currentWeb.Update();

                        }
                    }); // end elev
                }
            }


Now its worth noting that the title does indeed change (lol), but the view will 
always remain the embedded view from the web part.

Has anyone done something simular or have a work around at all? I've event 
tried blanking / replacing the listViewWP.XmlDefinition with 
DestListView.HtmlSchemaXml and  DestListView.GetViewXml() without much joy.

Thanks in Advance,

Daniel
_______________________________________________
ozmoss mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss

Reply via email to