Edit Web Parts through Sharepoint API

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?





jgleinserAsked:
Who is Participating?
 
Ted BouskillConnect With a Mentor Senior Software DeveloperCommented:
Have you tried?

DVwebpart = (DataViewWebPart)manager.WebParts["Client Documents"];
0
 
Ted BouskillSenior Software DeveloperCommented:
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
 
jgleinserAuthor Commented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
jgleinserAuthor Commented:
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
 
jgleinserAuthor Commented:
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
 
Ted BouskillSenior Software DeveloperCommented:
Then I can only assume you are using the incorrect name for the web part.
0
 
MsShadowCommented:
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
 
jgleinserAuthor Commented:
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
 
Ted BouskillSenior Software DeveloperCommented:
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
 
MsShadowCommented:
Don't you have to update the webpart itself as well?
0
 
jgleinserAuthor Commented:
@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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.