Thursday, April 16, 2015

Exception Trying to use an SPWeb object that has been closed or disposed and is no longer valid


A common mistake that people make in their code with this exception.
SPWeb, SPSite objects leak memory if they are not disposed properly.
The problem is so much talked about that as soon as we think of SPSite and SPWeb we unconsciously put using (SPWeb spweb = blah blah)
However when you use SPContext to get site or web object you should not be using “USING” or you should not dispose that object.  SPContext is passed to another web part on the page and SharePoint will dispose it when done.
Hence if you have been using (SPSite spsite = SPContext.Current.Site) it is wrong.  It might not always break as you might just have one web part in your code but you will see it break as soon as you add another web part and may be try to check in the page.

so change
using (SPSite spsite = SPContext.Current.Site)
to
SPSite site = SPContext.Current.Site.

Friday, March 27, 2015

Export to Excel issue - no events work

Post Back does not work after writing files to response or
After Export To Excel Button Event No Events Firing In Sharepoint?


I had this issue with sharepoint. I have a button on the page that sends a file and after clicking the button, the rest of the form was unresponsive. Turns out it is a sharepoint thing that sets the variable _spFormOnSubmitCalled to true to prevent any further submits. When we send a file this doesn't refresh the page so we need to manually set this variable back to false.
On your button in the webpart set the OnClientClick to a function in your javascript for the page.<asp:Button ID="generateExcel" runat="server" Text="Export Excel"OnClick="generateExcel_Click" OnClientClick="javascript:setFormSubmitToFalse()" />
Then in the javascript I have this function.
<script language="javascript" type="text/javascript">
    function setFormSubmitToFalse() {
        setTimeout(function () { _spFormOnSubmitCalled = false; }, 3000);
        return true;
    }
</script>
The 3 second pause I found was necessary because otherwise I was setting the variable before sharepoint set it. This way I let sharepoint set it normally then I set it back to false right after.

Thursday, March 12, 2015

SharePoint Performance Improvement with custom applications

Notes:

The following documented resources from the Internet are applicable to, or have been used to compile and validate the content of, this document:
·         SharePoint Developer Center: http://msdn.microsoft.com/en-us/sharepoint/default
·         Best Practices with SharePoint Foundation: http://msdn.microsoft.com/en-us/library/ee556427(office.14).aspx 



SharePoint Performance improvement:
1. Impersonation can significantly affect performance and scaling. It is generally more expensive to impersonate a client on a call than to make the call directly. 

3. Using PageAsyncTask to improve the performance of your website
     
5.    SharePoint 2010 Performance – Improvement Steps
a.    Use XSLT instead of grids.
b.    Disable view state if you are not performing any use actions.
c.    Don’t throw exceptions; re throw exceptions are much costlier in terms of memory consumption and execution.
d.    Minimize exception handling blocks, use exception block where ever you are not able to handle or pretend exceptions.
e.     
6.    Don’t run production ASP.NET Applications with debug=”true” enabled
a.    If you publish using Debug mode, your generated files have debugging enabled and that will impact the performance.
b.     
  

Wednesday, February 25, 2015

Create a Custom Master Page for SharePoint 2013 for Teams/Publishing Site Collection



I have successfully created a custom masterpage for SharePoint 2013 using Visual Studio 2013. Here are the steps I did to create my custom masterpage:Open the 15 hive to directory "15\TEMPLATE\GLOBAL"
  1. Copy the "seattle.master" file (or the oslo.master file)
  2. Create a new directory on your C: and paste the file
  3. Rename the file to anything you want. (i.e. seatle_k.master)
  4. Open Visual Studio 2013
  5. Create a new empty project
  6. Add a new module (2 files are created inside the module automatically.)
  7. Change the name of the module (i.e. ModuleMP)
  8. Delete the sample.txt file that was created automatically by adding the module
  9. Add an existing file to the module. Browse to your new seatle_k.master file and add it.
  10. Open the elements.xml file in the module.
  11. Change the <Module ...> by adding "List=116" and Url="_catalogs/masterpage"
  12. Change the <File ...> by adding Type="GhostableInLibrary"

    <?xml version="1.0" encoding="UTF-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Module Url="_catalogs/masterpage" List="116" Name="ModuleMP">
    <File Url="ModuleMP/seattle_kcpl.master" Type="GhostableInLibrary" Path="ModuleMP\seattle_k.master"/>
    </Module>
    </Elements>

  13. Change the name of "Feature1" to something more useful (i.e. K_Feature1)
  14. Double click on Feature1 (now K_Feature1) and add custom text to the title and description.
  15. Double check that the project will deploy to the correct site collection URL by checking the "Site URL"
  16. Build your project
  17. Deploy your project
  18. Assuming everything went well your project should be successfully deployed.
    (In my case it errored out with "unable to activate feature". then I created the package wsp and deployed with powershell, Add-SPSolution then manually deployed it from Central Admin.)

    If this is a publishing site collection, you can change the new masterpage from Site Settings -->Master page and Page layout under look and feel. If this is Team Site Collection then pls follow the below steps.
  19. Open the destination site collection and go to the site settings. Check site features and double check that the solution was added and automatically activated.
  20. Open SharePoint Designer 2013
  21. Open the destination site where you deployed your new acme.master file
  22. In the left-hand vertical navigation in SPD click on the link "Master Pages"
  23. You should see your new acme.master file in the list
  24. Right-click on the acme.master file and click link "Set as Default Master Page"
  25. Ignore and click 'OK' when a pop-up message telling you that there is no associated html file to your masterpage.
  26. Navigate to your destination site in Internet Explorer to see your new masterpage


Monday, February 16, 2015

Find PID for IIS Application Pool’s Worker Process


This blog post will show the different methods to identify the process ID (PID) for an active worker process (w3wp.exe) for an application pool
Method 1: - Internet Manager UI
  • Open IIS Manager
  • In the Connections pane, select the server node in the tree
  • In Features View, double-click Worker Processes
  • View the list of worker processes in the grid
Method 2: - Appcmd
Appcmd is used to list out the running worker processes. The path to the utility is not set and running directly from the command prompt will fail. Use the following instructions to set the path for the utility.
Location of appcmd.exe %systemroot%\system32\inetsrv
64-bit Windows, use Appcmd.exe from the %windir%\system32\inetsrv directory, not the %windir%\syswow64\inetsrv directory.
Set the path to the location
  • Start -> Computer -> Right Click ->Properties
  • Advanced System Settings -> Environment Variables
  • System Variables -> Path -> Edit
  • Add C:\Windows\System32\inetsrv
To start Appcmd.exe
  • Click Start, and then click All Programs.
  • Click Accessories, and then click Command Prompt.
  • At the Command Prompt, > cd %windir%\system32\inetsrv, and then press ENTER if the path is not set. Otherwise, enter appcmd on the command line
To List the started worker processes
>appcmd list wps
clip_image001
For complete listing of appcmds Listing of IIS 7+ Appcmd
Method 3: - Task Manager
  • Start Task Manager
  • Select the Processes tab
  • Add the following columns from View->Select Columns
    • PID
    • Command Line
  • Sort the Image column and find the w3wp.exe process name
  • Expand the Command Line column and the worker process name is on the far right
Method 4: - Process Explorer
  • Find the svchost.exe in the Process column and the w3wp.exe processes are listed
  • Expand the Command Line column and the worker process is listed
clip_image002

Thursday, January 22, 2015

Powershell Commands



New ULS Logs file
Use the below command when ever you want to create a new new Log file.
This is very helpful while investigating an issue. To get ULS logs for 30 seconds, execute the below command twice at starting and end of 30th second, so you will have a separate log file for the time period.
New-SPLogFile


Merge URLs Logs:
Combines trace log entries from all farm computers into a single log file on the local computer.

Merge-SPLogFile -Path <String> [-Area <String[]>] [-AssignmentCollection <SPAssignmentCollection>] [-Category <String[]>] [-ContextFilter <String[]>] [-Correlation <Guid[]>] [-EndTime <DateTime>] [-EventID <String[]>] [-ExcludeNestedCorrelation <SwitchParameter>] [-Level <String>] [-Message <String[]>] [-Overwrite <SwitchParameter>] [-Process <String[]>] [-StartTime <DateTime>] [-ThreadID <UInt32[]>]