menu
Tech blog, musings & updates from Intelligent Decisioning
Want to work with Intelligent Decisioning? That’s great, let’s talk

Category Archives: SharePoint

Enable Stack Traces to find C# Disposal Issues

Share socially -

If you look through your SharePoint ULS logs during the development of server-side code and you spot the warning:

An SPRequest object was not disposed before the end of this thread.  To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it.

Then there’s a chance you haven’t followed Microsoft’s best practices around disposal of unmanaged resources in the C# SharePoint Server Side Object Model.

Going solely by the information in ULS doesn’t immediately show you where your SPSite or SPWeb was instantiated.  As the warning states, you need to enable SPReqest Allocation Callstacks to see full stack traces of your code to see where you need to fix SPRequest issues.

The following PowerShell snippet will do just that:

$cs = [Microsoft.SharePoint.Administration.SPWebService]::ContentService

$cs.CollectSPRequestAllocationCallStacks = $true
$cs.Update()

 

Of course, as you develop you can use static analysis tools such as SPCAF from Rencore to make sure you’re following the best practice for object disposal as you code.

Please note that this is only beneficial if you develop C# code using the SharePoint Server Side Object Model that runs on the SharePoint servers. If you are developing using any of the remote APIs (such as JavaScript Object Model, Client Object Model or REST) then this setting has no real benefit.

You may also notice that sometimes you will see this error when performing valid API tasks where you do not necessarily create the SPWeb or SPSite objects yourself. I’ve noticed this in particular with the Taxonomy API. I found these to be acceptable exceptions when analysing the ULS log for these warnings, as the code that raises the warning is deep within assemblies that are maintained by Microsoft, and the warnings may disappear over time with service pack releases.

How to create an exportable Script Editor web part in Office 365

Share socially -

The Script Editor web part is one of my favourite tools in my SharePoint development kit as it enables developers and designers to be able to inject HTML, JavaScript and CSS into any SharePoint page without having to open SharePoint Designer.

Great, but… there is a drawback, and a quite annoying one at that… you cannot export a default Script Editor web part.

No way, no export!
No way, no export!

Why is that annoying? Exporting web parts is the easiest way to replicate the functionality configured inside them in SharePoint. You can take your exported web part, store it in your Site Collection Web Part Gallery and reuse it using the Add Web Part button on the SharePoint Ribbon.

However, there is a very simple update you can make to change this and create your own version of the Script Editor that you can export by default.

Add a Script Editor to a web part zone and click the Edit Web Part command from the web part menu. This will open the Web Part Properties pane.

On the properties pane, expand the Advanced section. You will see that there is a field to set the Export Mode of the web part, change this to Export All. This will enable the Export command on the web part menu, which will allow you to save the web part locally to your desktop.

advancedproperties
Export All the things!

After saving the web part locally, rename the file to “ExportableMSScriptEditor.webpart”. You can save this back to you SharePoint site for it be reused by uploading it to your Web Part Gallery.

Go to your site and click the Settings cog to access the Site Settings page, from here you can access the Web Part Gallery under the Web Designer Galleries heading.

Upload your new web part file into the Web Part Gallery using the Ribbon, on the Files tab click the Upload Document button and select the file when promoted by the “Add a web part” dialog box.

Once uploaded, you will be asked to confirm or alter the file name, title, description and group of the web part.

Once that is complete and your new web part is showing in the Web Part Gallery, you can now add this to your page via the Ribbon.

Edit the page, got to the Ribbon and select the Insert tab, click the Web Part button to open the Web Part Picker to find and select your web part to then add to the page.

View the web part in the Web Part Picker
View the web part in the Web Part Picker
It lives!!
It lives!!

You will now be able to Export the web part from the web part menu.

Now that you can use your own version of the Script Editor web part you can create web part functionality and easily export them for further reuse or simply for a quick backup, just in case someone changes and breaks it.

Here is an example of a web part that used the Exportable Script Editor web part as a starting point to display a simple list of links.

usefullinks

 

 

 

I hope you’ve found this little trick helpful and thank you for reading 😀