Sharepoint sites may contain few webparts which exist in all its subsites commonly.
This article is used to find the webpart and synchronize their stying using css styles instead of inline styling which needs lots of manual work even for a small change.
Content editor webpart may used used in all sharepoint sites. One content editor webpart may be used all the over the sites, sub-sites with different attributes specific to the sub-sites. Once the webpart is added in all the subsites and the specific attributes are set, at times each site may have different inline styling.
Now, the user may request for a common styling for all the webparts all over the sharepoint site.
Manually going to each sub-site, checking for this specific webpart and removing the styling needs lots of manual work and patience.
Using this powershell script, we could achieve this in fraction of seconds by just following the steps below.
Note: User should have Administrative rights to run this code.
This article helps you to insert a css style in the content editor webpart in all the sites, sub-sites whereever the webpart is present which makes us to easily maintain the styling at one go using powershell scripting and css style sheet.
Using the code
Step 1 :
Get the content editor webpart name and the details of the html content.
Example : Content editor webpart with buttons to Join in that specific subsite
Analyse the html content of the content editor webpart.
Different styles are applied to each button.
Changing the style for one webpart is very easy. But changing the style in hundreds of sub-sites is not so easy.
Each inline stying is inserted in the image tag <img>. The styling is present within the property "style="
Now we need to synchronize this style with a common class from CSS style sheet.
Insert a new style class in the css which you are using for the sharepoint site as below :
Step 4 :
Before going ahead, we need to know where this webpart is located in all the sub-sites.
In this example, this webpart is located in default.aspx page in all the sub-sites.
Now, create the powershell script to find the specific webpart in each sub-site.
Step 5 :
Find the default.aspx page of each site and find the specified webpart.
Example : Join the Sub Community
Once the page is found, we need to check the status of the page if it is checkedout to anyone else.
If checked out to others, editing the page and its content may not be possible.
If the page is not in checkedout status, follow your action of inserting a new css style in the webpart as follows.
Step 6 :
This code is used to find the image tag and then the style attribute.
Once the style attribute is found, it removes the inline styling and inserts our new class ".linkimg"
Page is checked out before making our changes. Page is checked in after the changes are made.
Step 7 :
This function is to be called by giving the site url and the location for saving the log as below :
This .ps1 script makes the required changes in all the web parts in all the subsites at one go.
Randomly, go to each sub site and check the content editor webpart's content.
A new class will be inserted instead of inline styling.
This way of coding makes easy to change the styling using CSS class.
Change the CSS class if you require any changes to be done, which would make the required changes all over the webparts in all subsites.
This completes changing the styling of a webpart all over the sub sites using powershell script.
You may extend this to make any kind of changes in the html content of the webparts.