c# - Modify Sharepoint non publishing default page content programmaticaly -
i need modify content of default.aspx page of on 100 sharepoint 2010 sites make slight change in content of page.
because of number of sites, programmatically, either in powershell, or through programming console application or along lines.
i've found plenty of sites/blogs describe how default page in pages library, doesn't work me though sites have publishing feature activated, there're no files in pages library, , far can tell it's not wiki page.
the issue i'm trying fix in listviewxml tag of lists on homepage isn't formed right. has 2 ?'s (query strings) in url, , want replace every instance of "?rootfolder=" "&rootfolder="
so far i've tried running c# console , i've been able locate default.aspx file, content isn't of content, structure.
using (spsite sitecollection = new spsite("my_site_url")) { spwebcollection sites = sitecollection.allwebs; foreach (spweb site in sites) { try { spfile file = site.getfile(site.url + "/default.aspx"); system.io.stream mystream = file.openbinarystream(); system.io.streamreader reader = new system.io.streamreader(mystream); string text = reader.readtoend(); console.writeline(text); } { if (site != null) site.dispose(); }
i'm outputting text console testing purposes. i'm hoping can string replace, , write content file , save somehow.
the solution doesn't have in c#, need automated way of making changes on many site home pages , seemed route promise. i'm manually opening page in sharepoint designer, doing find replace , clicking save button, tedious.
after lot more searching , head banging able resolve issue.
found out i'm not able modify code of page. needed modify xml of lists on pages themselves, xml of toolbar of list, , more toolbar of view.
most of following code came http://www.sharepointchick.com/archive/2009/07/24/programmatically-adjusting-the-toolbar-of-a-listviewwebpart.aspx
final code did trick me is:
spsite sitecollection1 = new spsite(<site>); spweb mywebs = sitecollection1.openweb(); foreach (spweb webitem in mywebs.webs) { spfile myfile = webitem.getfile("default.aspx"); splimitedwebpartmanager limitedwebpartmanager = myfile.getlimitedwebpartmanager(system.web.ui.webcontrols.webparts.personalizationscope.shared); // web parts available on default.aspx page splimitedwebpartcollection webparts = limitedwebpartmanager.webparts; // loop through collection of web parts on page foreach (system.web.ui.webcontrols.webparts.webpart webpartonpage in webparts) { // try set toolbar type web part listviewwebpart, other web part types don't have toolbars if (webpartonpage.gettype().name == "listviewwebpart") { listviewwebpart listviewwebpart = webpartonpage listviewwebpart; // view used in web part using reflection propertyinfo viewprop = listviewwebpart.gettype().getproperty("view", bindingflags.nonpublic | bindingflags.instance); spview webpartview = viewprop.getvalue(listviewwebpart, null) spview; // necessary after infrastructure update, without can't xml of view webpartview.gettype().invokemember("ensurefullblownxmldocument", bindingflags.nonpublic | bindingflags.instance | bindingflags.invokemethod, null, webpartview, null, system.globalization.cultureinfo.currentculture); propertyinfo nodeprop = webpartview.gettype().getproperty("node", bindingflags.nonpublic | bindingflags.instance); xmlnode node = nodeprop.getvalue(webpartview, null) xmlnode; // toolbar node xml of view used in web part xmlnode toolbarnode = node.selectsinglenode("toolbar"); if (toolbarnode != null) { toolbarnode.innerxml = toolbarnode.innerxml.replace("?rootfolder", "&rootfolder"); webpartview.update(); } } } }
Comments
Post a Comment