?
Solved

Edit Web Parts through Sharepoint API

Posted on 2009-12-31
11
Medium Priority
?
854 Views
Last Modified: 2012-05-08
I have several dozen subsites deployed as templates. I'm trying to edit XSL in a DataViewWebPart located on the default.aspx page in each site. I'm on attempt number 5 or 6 and could a hand.

If I find the web part using GetWebPartCollection or  GetLimitedWebPartManager I have to cast the WebPart to a DataViewWebPart before I can edit XSL. The casting has me stumped. Is this the best method?

I assume I can reference the webpart by  GetLimitedWebPartManager.WebParts["Part Name"] but I'm really trying to get to that XSL in the DataViewWebPart.

Any ideas on this cast or a better method?





0
Comment
Question by:jgleinser
  • 5
  • 4
  • 2
11 Comments
 
LVL 51

Expert Comment

by:Ted Bouskill
ID: 26158517
You are on the right track and it's the only way.  Can you give me a code snippet for where you are having problems with the casting?
0
 

Author Comment

by:jgleinser
ID: 26158587
SPSite onesite2 = new SPSite(item["URL"].ToString());
SPWeb oneweb2 = onesite2.OpenWeb();
onesite2.AllowUnsafeUpdates = true;

SPLimitedWebPartManager manager = oneweb2.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);
                                       
DataViewWebPart DVwebpart = new DataViewWebPart();
DVwebpart = manager.WebParts["Client Documents"];

At this last line I receive the error that I cannot cast a WebPart to a DataViewWebPart, but that an explicit conversion exists. I can't find this conversion anywhere!                                                                            
0
 
LVL 51

Accepted Solution

by:
Ted Bouskill earned 2000 total points
ID: 26158630
Have you tried?

DVwebpart = (DataViewWebPart)manager.WebParts["Client Documents"];
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 

Author Comment

by:jgleinser
ID: 26159118
The casting appears to work though when I attempt to reference the new instance I'm getting an "Object Reference not set to an instance of an object" error.

 DVwebpart = (DataViewWebPart)manager.WebParts["Client Documents"];
 w.WriteLine(current.ToString() + " DVwebPart.Xsl: " + DVwebpart.Xsl, w);

Any idea?

0
 

Author Comment

by:jgleinser
ID: 26159129
DataViewWebPart DVwebpart = new DataViewWebPart();
DVwebpart = (DataViewWebPart)manager.WebParts["Client Documents"];
 w.WriteLine(current.ToString() + " DVwebPart.Xsl: " + DVwebpart.Xsl, w);

I probably should show you the first line too to answer that question.
0
 
LVL 51

Expert Comment

by:Ted Bouskill
ID: 26159691
Then I can only assume you are using the incorrect name for the web part.
0
 
LVL 19

Expert Comment

by:MsShadow
ID: 26170223
Going with what Ted says, Object Reference not set to an instance of an object means that your webpart = null which means that it's not getting filled with the webpart object, and that happens when the name is not correct. Don't give me assistance points for this, Ted is spot on.
0
 

Author Comment

by:jgleinser
ID: 26175846
You were correct. I am only able to reference the web part via an index int and not the actual name. I'm not sure why that is, but I'm able to predict the index for this purpose.

Everything is humming along except for the actual XSL update. I apply the new xsl via:

SPWeb oneweb2 = onesite2.OpenWeb();
onesite2.AllowUnsafeUpdates = true;
DFwebpart = (DataFormWebPart)manager.WebParts[2];
DFwebpart.Xsl = newxsl;
oneweb2.Update();

The XSL never changes though. Any ideas?
0
 
LVL 51

Expert Comment

by:Ted Bouskill
ID: 26177272
I suspect that it is because you are working with a copy of the web part and the change isn't reflected by to the page.

Just for the fun of it try this:
((DataFormWebPart)manager.WebParts[2]).Xsl = newxsl;


0
 
LVL 19

Expert Comment

by:MsShadow
ID: 26178660
Don't you have to update the webpart itself as well?
0
 

Author Comment

by:jgleinser
ID: 26181153
@tedbilly: Attempted with same result. The XSL does not change though there is not an error.

@MsShadow: I don't see any Update method on that object. However I did find the SaveChanges method on the LimitedWebPartManager. This successfully updated the XSL.

For posterity's sake, here is the final code that updated XSL on a DataFormWebPart through the Sharepoint API.

SPSite onesite = new SPSite(URL);
SPWeb oneweb = onesite.OpenWeb();
onesite.AllowUnsafeUpdates = true;
SPLimitedWebPartManager manager = oneweb.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);
DataFormWebPart DFwebpart = new DataFormWebPart();
DFwebpart = (DataFormWebPart)manager.WebParts[2];
DFwebpart.Xsl = newxsl;
manager.SaveChanges(DFwebpart);
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When installing SharePoint 2010 RTM I came across a strange error, I was getting timeouts during the installation. I searched the web and found the best solution to be found here (http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010genera…
Pimping Sharepoint 2007 without Server-Side Code Part 1 One of my biggest frustrations with Sharepoint 2007 in the corporate world is that while good-intentioned managers lock down the more interesting capabilities of Sharepoint programming in…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question