Saturday, September 12, 2015

Trigger a SharePoint Designer Workflow after Publishing the document

Hi folks,
Today i am going to give a work around solution for common requirement that we face from our clients on content approval and designer workflow triggering.

Case: Client is having a document library with custom SharePoint designer workflow. Content approval is activated on this library. Which means, you will see Check out, Check in, Publish, Approve, Reject options.

User requirement is, the custom designer workflow (any logic u wish)  should fire only after user hits publish button of an item.

Solution: OOB we dont have such workflow triggering features. We have only On Item Created and On Item Updated. So i decided for a workaround by using On Item Updated option. It is by using "Approval Status" column.

When you enable content approval, you will see a new OOB column called "Approval Status" which will be usually having status "Draft", "Pending", "Approved", "Rejcted". The steps are..

1. Make your designer workflow trigger on every item update
2. Add a first step/stage ( SP 2010/SP2013) to decide the "Approval Status" value.
3. Create a workflow variable of type string and name it as "CurrentApprovalStatus" 
4. Set workflow variable "CurrentApprovalStatus" to "Current Item:Approval Status"
5. drop If action and check If CurrentApprovalStatus equals 2 then go to required stage of yours. Usually it will be your initial stage of workflow. Else Go to End of workflow.

Note: We are comparing with 2 as the status after clicking publish will be "Pending" which is internally 2

FYI: The other values of Content "Approval Status" column are:
0 - Approved
1 - Rejected
2- Pending
3- Draft
4- Scheduled


Basically i wrote the explanation by considering SP 2013 designer workflow. I believe there will be slight changes in Action names for SP 2010 workflows.

If any suggestions/corrections, most welcome....

thank you

Friday, April 10, 2015

Url Properties of SPSite and SPWeb



There are some properties for SPSite and SPWeb class using which we can get the URL values for the sites and webs. The two most used of these properties are - 'ServerRelativeUrl' and 'Url'
As a developer we use the values of the properties for writing some logic for satifying the business needs. Whenever I used to write a OM code to use these properties, the first question which flashes in my mind - "What values are returned by these properties?". I immediately create a cosole application, and check the values returned by these properties and contiue with the actual work. I have seen many other developers doing this. So, for a quick reference I prepared the below table which has the url values returned by these properties.
Some poeple may find this easy, but some (like me ;)) get confused when the sites have managed paths. These properties return the managed path if the site has one. Here goes the list
SCENARIO 1: A Site Collection (SPSite) without any managed path.
URL : 'http://rams'
PropertyValue
SPSite.ServerRelativeUrl/
SPSite.Urlhttp://rams
SPWeb.ServerRelativeUrl/
SPWeb.Urlhttp://rams
Url - http://rams/about ('about' is a sub site)
SPSite.ServerRelativeUrl/
SPSite.Urlhttp://rams
SPWeb.ServerRelativeUrl/about
SPWeb.Urlhttp://rams/about
This looks pretty simple. We get confused when sites are created using managed paths. Below are the samples values for site created with managed paths
SCENARIO 2: A Site Collection with a managed path (of type Explict inclusion). For the below URL '/my' is the managed path
URL : 'http://rams/my'
PropertyValue
SPSite.ServerRelativeUrl/my
SPSite.Urlhttp://rams/my
SPWeb.ServerRelativeUrl/my
SPWeb.Urlhttp://rams/my
Url - http://rams/my/about ('about' is a sub site)
SPSite.ServerRelativeUrl/my
SPSite.Urlhttp://rams/my
SPWeb.ServerRelativeUrl/my/about
SPWeb.Urlhttp://rams/my/about

SCENARIO 3: A Site Collection with a managed path (of type Wilcard inclusion). For the below URL '/sites' is the managed path, and a site collection is created at '/blog'
URL : 'http://rams/sites/blog'
PropertyValue
SPSite.ServerRelativeUrl/sites/blog
SPSite.Urlhttp://rams/sites/blog
SPWeb.ServerRelativeUrl/sites/blog
SPWeb.Urlhttp://rams/sites/blog
Url - http://rams/sites/blog/about ('about' is a sub site)
SPSite.ServerRelativeUrl/sites/blog
SPSite.Urlhttp://rams/sites/blog
SPWeb.ServerRelativeUrl/sites/blog/about
SPWeb.Urlhttp://rams/sites/blog/about

Tuesday, April 7, 2015

How to make page active after download using Response in C# in sharepoint webpart/user control

I usually came across these kind of statements 'Page hangs after downloading', 'post back not happening after file download'

yes, its true, when we are trying to download a file from C# and with the logic of Response, the last statement of response will be response.end.

once the compiler hits this response.end, the page losts its response and hence, any button or post back controls wont work. It looks like the page and controls are numb. 

Think 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 javascript function like shown below.

 <asp:Button ID="generateExcel" runat="server" Text="Export Excel" 
OnClick="generateExcel_Click" CssClass="rptSubmitButton"
OnClientClick="javascript:setFormSubmitToFalse()" />
Then in javascript function write as below.
function setFormSubmitToFalse() {
    setTimeout(function () { _spFormOnSubmitCalled = false; }, 3000);
    return true;
}
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.