Friday 31 May 2013

Moving SharePoint 2010 Workflows

Moving SharePoint 2010 Workflows

There was an option in SharePoint Designer, Export to Visio which exports your workflow as a .vwi file, and can be imported in to another site using the option Import from Visio. But when you try that option, you will get the below message:

This workflow cannot be imported because it was created in SharePoint Designer for a different site, or the original workflow has been moved or deleted. To move a workflow between sites, use Save as Template (.wsp file) instead of a Visio workflow drawing.

(Saving as a .wsp IS NOT the answer)

So, to achieve the same follow the steps below.
  1. In the first(source) site, create the required workflow and publish it.
  2. Now select Export to Visio option which allows you to save the workflow with a .vwi extension. (Refer this workflow hereafter as source workflow).
  3. Now go to the destination site where you want the workflow to be copied, and create a new workflow with the same name as the previous one & publish it.
  4. Now select Export to Visio option which allows you to save the workflow with a .vwi extension. (Refer this workflow hereafter as Destination workflow).
  5. Now you will be having two .vwi files (one of source workflow’s – SourceWorkflowName.vwi and other of the destination workflow’s – DestinationWorkflowName.vwi). Now add .zip extension to both the files. Now your files names should be SourceWorkflowName.vwi.zip & DestinationWorkflowName.vwi.zip.
  6. Now open both the zip files, copy workflow.xoml.wfconfig.xml from destination workflow to source workflow. (Its destination to source and not source to destination).
  7. From now on, we will not use the file DestinationWorkflowName.vwi.zip. So ignore that file.
  8. Remove the .zip extension from SourceWorkflowName.vwi.zip which gives you the SourceWorkflowName.vwi file.
  9. Now, go to the destination site, open workflows and click Import from Visio and browse to the SourceWorkflowName.vwi file.
  10. That’s it and your workflow is copied. You can publish the workflow and run it.
PS : In case if your list’s GUID’s (for those lists that you have used in workflow – tasks list, history list or any other lists used in workflow steps) have been changed from source & destination site, you may need to update those steps in the workflow. Lookups will need to be rebuilt.

Tuesday 14 May 2013

Content DB backup and restore for site collection.

Content DB backup and restore for site collection through powershell commands.

1. Backup
stsadm.exe -o backup -filename "E:\SharePointBackups\filename_for_sitecollection" -overwrite -url  "url_of_sitecollection"

2. Restore
stsadm -o restore -url "<URL name>" -filename "<UNC path>"

Wednesday 8 May 2013

Quick Launch on a Web Part Page

We go to All Site Content > Create > Web Part Page> Create ! A WebPart Page is created. But notice something different about the page?

The Quick Launch menu is missing. Not good for those of us who want consistent local navigation throughout their site. But fear not! It can be restored with a few clicks in SharePoint Designer. Follow the steps below:

To enable this type of page you just follow these three steps:

  1. Open the page in SharePoint Designer 2010 (be sure to make a back up, in case things go arwy).
  2. Look for this code snippet in the Code View (Use Ctrl + F to search for it, but make sure your cursor is clicked in the "Code View" area, or you won't be able to use the search in source code option):

    <SharePoint:UIVersionedContent ID="WebPartPageHideQLStyles" UIVersion="4"runat="server">
    <ContentTemplate>
    body # {s4-leftpanel
    display: none;
    }
    . s4 {ca-
    margin-left: 0px;
    }
    </ style>
    </ ContentTemplate>
    </ SharePoint: UIVersionedContent>
  3. Delete the entire snippet.
  4. next, search for this place holder in the code:

    <asp:Content ContentPlaceHolderId="PlaceHolderLeftNavBar" runat="server">
    </ asp: Content>
  5. Delete the entire place holder.
  6. Save and Close.

The Quick Launch navigation should now be back! Try now reloading the page.

Programmatically create sharepoint group and assign permissions

Create sharepoint group programmatically and assign permissions to new group.
private void CreateSharepointGroup()
{
ArrayList groupList = new ArrayList();
groupList.Add(” Home Owners”);
groupList.Add(” Home Members”);
groupList.Add(“Home Contribute”);
groupList.Add(” Home Visitors”);

using (SPWeb oWebsite = SPContext.Current.Site.OpenWeb(“Website_URL”)) eg: (site1/Subsite2)
{
SPUser sUser = oWebsite.CurrentUser;
foreach (string groupName in groupList)
{
if (!ContainsGroup(oWebsite.SiteGroups, groupName))
{

oWebsite.SiteGroups.Add(groupName, oWebsite.SiteUsers[sUser.LoginName],
oWebsite.SiteUsers[sUser.LoginName], “Use this group to give users to give permissions to the SharePoint site”);

if (groupName.Contains(“Owners”))
{
SetRoleDefinitionBinding(groupName, oWebsite, “Full Control”);
}
else if (groupName.Contains(“Members”))
{
SetRoleDefinitionBinding(groupName, oWebsite, “Limited Access”);
}
else if (groupName.Contains(“Contribute”))
{
SetRoleDefinitionBinding(groupName, oWebsite, “View Only”);
}

else if (groupName.Contains(“Visitors”))
{
SetRoleDefinitionBinding(groupName, oWebsite, “View Only”);
}
}
}

}

}

// Assign permissions
private void SetRoleDefinition(string groupName, SPWeb oWebsite, string permissionLevel)
{
// add the read role definition to the site group

SPRoleAssignment roleAssignment = new SPRoleAssignment(oWebsite.SiteGroups[groupName]);

roleAssignment.RoleDefinitionBindings.Add(oWebsite.RoleDefinitions[permissionLevel]);

oWebsite.RoleAssignments.Add(roleAssignment);

oWebsite.Update();
}

// check whether group exist or not
private bool ContainsGroup(SPGroupCollection groupCollection, string index)
{
try
{
SPGroup testGroup = groupCollection[index];
return true;
}
catch (SPException e)
{
return false;
}
}

Create Site/Sub Site using Custom SiteTemplate in Sharepoint programatically

Programmatically create Sites and Site Collections based on Site Templates.


using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb parentWeb = site.OpenWeb())
{
string webTitle = txtSiteName.Text.ToString().Trim(); // Site Name
 
string webDesc = “Site Description”;
 
string webName = txtSitepath.Text.ToString().Trim(); // URL to new Site
 
string webUrl = string.Empty;
 
if (string.IsNullOrEmpty(parentWebName))
{
webUrl = webName;
}
else
{
webUrl = parentWebName.ToString().Trim()+”/”+webName;
}
 
uint webLcid = parentWeb.Language;
SPWebTemplate webTemplate = null;
// site.GetWebTemplates((uint)1033); OR site.GetCustomWebTemplates((uint)1033); From Root Site

foreach (SPWebTemplate wt in parentWeb.GetAvailableWebTemplates((uint)1033))
{
if (wt.Title == “Collaboration Meeting Site Template”) // template name
{
webTemplate = wt;
break;
}
}
 
// Name value for the Document Workspace template.
// Create the new web.
SPWeb newWeb = null;
try
{
newWeb = site.AllWebs.Add(webUrl, webTitle, webDesc, webLcid, webTemplate, true, false);
lblMessage.Text = “Sub Site ” + webTitle + ” Successfully Created !! ;
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
 
// Add a link to the new web on the RootWeb’s topnav bar.
if (newWeb != null)
{
// Set the new web’s top link bar to inherit links from its parent web.
newWeb.Navigation.UseShared = true;
 
// Create a link pointing to the new web.
SPNavigationNode node = new SPNavigationNode(newWeb.Title, newWeb.ServerRelativeUrl);
 
// Find out if the parent inherits links.
bool parentInheritsTopNav = newWeb.ParentWeb.Navigation.UseShared;
if (parentInheritsTopNav)
{
// Add the link to the top link bar on the root web.
site.RootWeb.Navigation.TopNavigationBar.AddAsLast(node);
}
else
{
// Add the link to the top link bar on the parent web.
newWeb.ParentWeb.Navigation.TopNavigationBar.AddAsLast(node);
}
newWeb.Dispose();
}
}
}

Wednesday 1 May 2013

Creating List Workflow from Sharepoint Designer

Creating a List Workflow


Now we can experiment with creating a Workflow using SharePoint Designer. Following are the steps involved.

Step 1: Create Team Site

Open SharePoint Designer from the Start menu. Click on the Team Site button and enter the name of the new web site (mynewsite), as shown below.



This step will create a new site with a Tasks list.

Step 2: Create Workflow

Select the Workflows item from the left side Navigation pane.



Click on the List Workflow button from the ribbon and click the Tasks item.



In the appearing dialog, enter the name of the new Workflow as shown below.



Step 3: Create Condition and Action

Now we need to specify the Condition and Action of the item. Each Condition will have one or more Action items associated with it.

The Condition will be evaluated and the corresponding Actions will be executed. The Condition could be checking a field property of the list or library, checking the user name, etc.

The Action is the actual performing item. The Action could be updating a field of the current item, moving the list item to another list, emailing a user, adding a comment, checking out a document, invoking another workflow, etc.

You can create Condition and Action using the ribbon items.



For creating the condition, use Insert > Condition > If current item field equals value. Your Workflow will look like below. The links field and value has to be specified.



Click on the links above and set the links as shown below:


Now click the Insert > Actions > Set field in current item. Modify the comment as Good Job Dude! as shown below.


Click on the Save button and we are ready with the Condition and Action. The condition and Action checks the item of the Task and if the Status is Completed, the Description column is set to a message.

Step 4: Change Workflow Settings

Go to the Navigation > Workflows list and use the Workflow Settings menu item.



In the Start Options section on the right, do the following:
  1. Uncheck the Allow this workflow to be manually started item
  2. Check the other two items for ensuring automatic execution of the Workflow
This ensures that our workflow will be automatically executed on insert/update of the Task item in the Task List.

Click on the Save button and then click on the Publish button.



Now our workflow is ready and deployed to the SharePoint site named mynewsite.

Step 4: Test the Workflow

We can test the workflow by opening the above site in browser.



Click on the Tasks list and create a new item. Set the Status of the Task to completed. Leave the Description field as empty as shown below. Click on the Save button.



You can see that a new column for the Workflow appeared for the above task item. Workflows will be adding their own columns to the context list. Refresh the page and wait until the Workflow status is Completed.


Now try viewing the item again and you can see that the Description has changed to our message.



Congratulations! You are done with your first workflow.