31. Steps to
debug SharePoint Timer Job ?
Its pretty simple. Follow the following step to
debug the Timer Job.
1. Open the Visual Studio and the Timer Job code
base
2. Set the break point in the code
3. Attach the debugger to OWSTimer.exe
4. Wait for the next cycle of the timer job.
Make sure that you don’t set Timer Job interval more
than 5 min for development purpose, otherwise you need to wait for longer.
You can also execute the Timer Job forcefully by
writing a small console application.
or
using STSADM command line
Code:
stsadm -o
execadmsvcjobs
Other Consideration:
When you change the code base for the Timer Job,
deploy the new binary in GAC and RESET the OWSTimer.exe service.
Yes you are right I said OWSTimer and not IIS Reset
, because timer job run inside the OWStimer process only and till you restart
it, it will always run it from previously cached copy .
32. Steps to debug
SharePoint Solution ?
Debugging code is as important as writing the code.
There are few differences in debugging SharePoint code as compared to
traditional ASP.NET applications. In ASP.NET you can just set the breakpoint in
the code. Press F5 and you can debug the code line by line. It is not as simple
in SharePoint because you already have a web application up and running to
which you are deploying your customization.
Following are the various techniques to debug code
in SharePoint.
1.Debugging
in Visual Studio
Compile your solution and deploy it (Don't press
F5).
Select Tools –> Options from Visual Studio and
uncheck “Enable Just My Code (Managed only)" check box.(Some server may
work without doing this step so let see how it goes). Select Tools –> Attach
to Process from Visual Studio and select w3wp.exe worker process you want your
debugger to be attached to.
2.Disabling
Custom Errors
3.Introducing
Developer Dashboard
To enable the developer dashboard execute the
following command through PowerShell:
stsadm -o
setproperty -pn developer-dashboard -pv OnDemand
The other options for this are On and Off in place
of OnDemand. After executing this command, refresh your site and you can see a
new icon on the top left after the username. Click it and you can see the
developer dashboard at the bottom of the same page.
33. Best SharePoint Coding
Practices ?
Some of the best coding practices that should be
followed in SharePoint development
Best Coding
Practice # 1
Do not
instantiate SPWeb, SPSite, SPList or SPListItem objects inside event receivers
using (SPWeb web
= properties.Site.OpenWeb("WebName"))
{
SPList list =
web.GetList("/Path");
SPListItem item = list.GetItemById(theID);
}
Best Coding
Practice # 2
Take advantage
of ‘using’ clause, because SPWeb and SPSite objects implement the IDisposable
interface, and standard .NET Framework garbage collection calls the Dispose
method to free any resources associated with the object from memory.
using(SPSite
oSPsite = new SPSite("http://server"))
{
using(SPWeb oSPWeb = oSPSite.OpenWeb())
{
str = oSPWeb.Title;
str = oSPWeb.Url;
}
}
Best Coding
Practice # 3
SPContext
objects are managed by the SharePoint framework and should not be explicitly
disposed in your code. This is true also for the SPSite and SPWeb objects
returned by SPContext.Site, SPContext.Current.Site, SPContext.Web, and
SPContext.Current.Web
using
( SPWeb web =
SPControl.GetContextWeb(HttpContext.Current)) { ... }
Best Coding
Practice # 4
SPWeb
object from AllWebs property collection is disposed
using (SPSite
siteCollection = new SPSite("http://moss"))
{
using (SPWeb web =
siteCollection.AllWebs.Add("site-relative URL"))
{
} // SPWeb object web.Dispose()
automatically called.
}
Best Coding
Practice # 5
It is not
advisable to call the Dispose method on the SPSite.RootWeb.
using (SPSite
siteCollection = new SPSite("http://moss"))
{
SPWeb rootWeb1 = siteCollection.RootWeb;
// No explicit rootWeb1 dispose required.
} // siteCollection automatically disposed by
implementing using().
// rootWeb1 will
be Disposed by SPSite.
// SPContext and
SPControl
SPWeb rootWeb2 =
SPContext.Current.Site.RootWeb;
// Also would
apply to SPControl.GetContextSite(Context);
// No explicit
rootWeb2 dispose required because it's obtained from SPContext.Current.Site.
Keeping in mind
of better ways of using SharePoint objects and best coding practices as above
will increase the performance of the application and also ensure the
reliability.
34. Update Vs. System
Update List in SharePoint 2013 ?
SPList lstProducts =
web.Lists.TryGetList("Products");
SPListItem prodItem = lstProducts.Items.Add();
prodItem["ProdName"] = "PCMMigrator";
prodItem["Category"] = "Sharepoint";
prodItem.Update();
//prodItem.SystemUpdate();
Update() :
In the above code the Update() method adds the data
to the SharePoint Products list. When this is happening it also internally
updates the built in fields the "Modified with the
timestamp of the server when it is updating" and "Modified By
with the user who logged in" . And also a new version is created for the
item (if versioning enabled for the list).
SystemUpdate() :
It updates the data to the SharePoint list without changing the built in Modified
and Modified By fields. It will not create any new version for the item.
Basically this method does all the updates to the content database directly.
This method also can be written with Boolean
argument.
SystemUpdate(false) is equivalent to SystemUpdate()
SystemUpdate(true) is equivalent to SystemUpdate()
but creates the new version for the item.
The SystemUpdate also triggers all the list events as the update does.
In Server Object Model we can use these both Update
and SystemUpdate methods. But the client object model will not allow the
SystemUpdate method as it is changing data in content db directly without
server knowing the changes made. If really we want to achieve this in csom,
there are some alternative ways like writing a custom webservice and calling it
in csom code.
35. What is SPDataSource and mode CrossList in SharePoint ?
SPDataSource is a web control which implements
IDataSource and saves you writing code like the above. The great thing is that
it is extremely flexible, and as we'll see, can be used for more than you might
think. The best thing though, is that being a control it can be used
declaratively, so I can bind my dropdown to the list without writing a single
line of C# or VB.Net code - all I have to do is set properties correctly.
<SPWebControls:SPDataSource
runat="server" ID="dsPersonTitles"
DataSourceMode="List"
SelectCommand="<Query><OrderBy><FieldRef
Name='SortOrder' Ascending='true' /></OrderBy></Query>"
<SelectParameters>
<asp:Parameter Name="WebUrl"
DefaultValue="/configuration/" />
<asp:Parameter
Name="ListName" DefaultValue="PersonTitles" />
</SelectParameters>
</SPWebControls:SPDataSource>
<asp:DropDownList
runat="server" ID="ddlPersonTitles"
CssClass="title" DataSourceID="dsPersonTitles"
DataTextField="Title" DataValueField="ID">
</asp:DropDownList>you
are right I sai
The different 'modes' of SPDataSource
SPDataSource isn't just limited to fetching the
items from a list (DataSourceMode = 'List'). Other possibilities are:
CrossList - similar to doing a query with
SPSiteDataQuery across all lists in a site collection (for a sample of this see
the SharePoint Designer Team blog post linked at the end of this article)
ListItem - show field values from a single list item
Webs - lists all webs in a site collection
ListOfLists - lists all lists in a web.
<SharePoint:SPDataSource
ID="SPDataSource1" runat="server"
DataSourceMode="CrossList"
UseInternalName="true"
SelectCommand="<Webs
Scope='Recursive'></Webs>
<Lists
ServerTemplate='106'></Lists>
<View>
<ViewFields>
<FieldRef
Name='EventDate'/>
<FieldRef Name='Title'/>
<FieldRef
Name='Location'/
</ViewFields>
<Query>
<Where>
<And>
<Eq>
<FieldRef
Name='Location'/>
<Value
Type='Text'>Your office</Value>
</Eq>
<Eq>
<FieldRef
Name='EventDate'/>
<Value
Type='DateTime'><Today/></Value>
</Eq>
</And>
</Where>
</Query>
</View>" >
</SharePoint:SPDataSource>
<asp:GridView ID="GridView1"
runat="server"
DataSourceID="SPDataSource1"
AutoGenerateColumns="false"
Width="75%" HeaderStyle-HorizontalAlign="Left" >
<Columns>
<asp:BoundField
HeaderText="Date" DataField="EventDate" />
<asp:BoundField
HeaderText="Title" DataField="Title" />
<asp:BoundField
HeaderText="Location" DataField="Location" />
</Columns>
</asp:GridView>
Thank you very much
Fahadullah Karimi
SharePoint Specialist
No comments:
Post a Comment