Add SharePoint workflow to a list programmatically - sharepoint

We are using SharePoint Foundation 2010.
We have created a workflow that checks the status of a task list.
If we associate the workflow with the list in the SharePoint UI it works fine.
We were wondering how we could automatically associate it, maybe in the feature receiver code that sets ut the site?

// 1. create an instance of the SPWorkflowAssociation class
SPWorkflowAssociation workflowAssociation =
SPWorkflowAssociation.CreateListAssociation(workflowTemplate, associationName, taskList, historyList);
// 2. set start options
workflowAssociation.AllowManual = true;
workflowAssociation.AutoStartChange = false;
workflowAssociation.AutoStartCreate = false;
// 3. set additional association options (if any)
workflowAssociation.AssociationData = associationData;
// 4. add workflow association to the list
list.WorkflowAssociations.Add(workflowAssociation);
// 5. enable workflow association, so it is displayed in the user interface
workflowAssociation.Enabled = true;

Related

Why does DocumentHelper.CopyDocument place the new document in "Published" workflow step?

I'm trying to copy a document using the Kentico 11 API and when using Document.CopyDocument the resulting TreeNode is automatically placed into the "Published" workflow step instead of "Edit" as one would expect. Is there any way to prevent this from happening?
I've tried both methods for DocumentHelper.CopyDocument including the method using CopyDocumentSettings, neither give the option to specify the initial workflow step.
TreeNode newDoc = DocumentHelper.CopyDocument(document, parentDoc, true, _treeProvider);
// also tried this
TreeNode newDoc = DocumentHelper.CopyDocument(new CopyDocumentSettings(document, parentDoc, _treeProvider));
Neither give me the option to specify workflow step, they only push it into "published". I can create a new version of the document, but that doesn't prevent the copy from being published initially.
I believe it copies the workflow status of the current page but I could be wrong. What I'd suggest is below. This code checks to see if the page is using check in/out and then uses workflow to set the newly created page to the first step in the workflow (which should be "edit").
TreeProvider tree = new TreeProvider();
var treeNode = DocumentHelper.GetDocument(4, tree);
var targetNode = DocumentHelper.GetDocument(5, tree);
CMS.DocumentEngine.TreeNode newPage = DocumentHelper.CopyDocument(treeNode, targetNode, false);
WorkflowManager workflowManager = WorkflowManager.GetInstance(tree);
WorkflowInfo workflow = workflowManager.GetNodeWorkflow(page);
if (!newPage.IsCheckedOut)
{
newPage.CheckOut();
}
if (workflow != null)
{
if (!workflow.WorkflowAutoPublishChanges)
{
newPage.MoveToFirstStep("Copying the page from another page.");
}
}
if (newPage.IsCheckedOut)
{
newPage.CheckIn();
}
I should have waited longer to post this question, turns out you can use the WorkflowManager in the API to push the document to the first step in the workflow:
newDoc.WorkflowManager.MoveToFirstStep(newDoc);
That pushes the document to the "Edit" step without affecting the workflow history, its as if the document was never published.

SharePoint 2013 Activity Event in Newsfeed

I need to add custom notifications to the personal Newsfeed on people's MySites. I found several tutorials and code examples for SharePoint 2010 on the net and tried to do the same with SharePoint 2013. They're all about creating ActivityEvents with the ActivityManager.
Here's the code I tried:
var targetSite = new SPSite("URL to MySite webapp");
SPServiceContext context = SPServiceContext.GetContext(targetSite);
var userProfileManager = new UserProfileManager(context);
var ownerProfile = userProfileManager.GetUserProfile("domain\\user1");
var publisherProfile = userProfileManager.GetUserProfile("domain\\user2");
var activityManager = new ActivityManager(ownerProfile, context);
Entity publisher = new MinimalPerson(publisherProfile).CreateEntity(activityManager);
Entity owner = new MinimalPerson(ownerProfile).CreateEntity(activityManager);
ActivityEvent activityEvent = ActivityEvent.CreateActivityEvent(activityManager, 17, owner, publisher);
activityEvent.Name = "StatusMessage";
activityEvent.ItemPrivacy = (int)Privacy.Public;
activityEvent.Owner = owner;
activityEvent.Publisher = publisher;
activityEvent.Value = "HELLOOOO";
activityEvent.Commit();
ActivityFeedGatherer.BatchWriteActivityEvents(new List<ActivityEvent> { activityEvent }, 0, 1);
The Id 17 in the CreateActivityEvent function is for the StatusMessage activity type, which is layouted like {Publisher} says: {Value} in the ressource files, so I provide the Value property of my ActivityEvent.
The code runs without any exception and in the User Profile Service Application_ProfileDB database I can see the right entries appear in the ActivityEventsConsolidated table.
But the activity is not visible in the activity feed, neither on the Owner's one, nor on the Publisher's one, even though these people follow each other. I ran the Activity Feed Job in the CA manually to update the activity feed.
Also, I tried to do the same with custom ActivityTypes with own ressource files, same result: The entry in the ActivityEventsConsolidated table (or ActivityEventsPublished if Owner=Publisher) appear, but no entries on the MySite.
Can anyone help?
I found the solution for this problem myself.
In Central Administration, Setup MySites, you have to enable the Enable SharePoint 2010 activity migration setting in the Newsfeed section in order to support SP1010 legacy activities in SP2013.

How to create a OOTB Workflow Tasks List

I am hearing about OOTB Workflow Tasks List, but I'm not being able to find any direct way to create a Workflow Tasks List (like a Tasks or a Project Tasks List). Is there any list definition for a Workflow Tasks list really?
The Workflow Tasks list created in a Publishing site, is an OOTB Tasks list.
http://mysharepoint.com/_layouts/new.aspx?FeatureId={00bfea71-a83e-497e-9ba0-7a5c597d0107}&ListTemplate=107
Here is the code from Microsoft.SharePoint.Publishing.Internal.ProvisioningHelper.CreateApprovalTaskList:
bool newListCreated = false;
list = ProvisioningHelper.AddList(
lists,
"WorkflowTasks",
"$Resources:cmscore,ListNameWorkflowTasks;",
"$Resources:cmscore,ListDescriptionWorkflowTasks;",
WssFeatureIds.TasksList,
(Guid[]) null,
SPListTemplateType.Tasks,
out newListCreated);
if (newListCreated)
{
list.EnableDeployWithDependentList = false;
list.AllowDeletion = false;
list.Update();
ProvisioningHelper.DisableCrawlOnList(list);
}

Is there some way in SharePoint to change document library views on a per-user basis?

We are using WSS 3.0 and I was asked to see if users can set default views on a per-user basis. Is anyone aware of any method (programatic or through the GUI itself) to give users the ability to change default views on a per-user basis? The 30 minutes of googling and poking around in the administrative menus turned out to be unfruitful. If not, is this a feature of MOSS 2007?
You probably want to look into audiences which is functionality in MOSS 2007.
Unfortunately it's not available in WSS 3.0
Here's a reasonable overview. User Profiles and Audience Targeting in SharePoint 2007
If you're working in WSS 3.0, you can programmatically swtich or modify views by using a webpart which gets the ListViewWebPart and modifies the query or view on the fly. Here is some sample code I am using to filter the contents of any given view:
private ListViewWebPart GetListViewWebPart()
{
ListViewWebPart webPart = new ListViewWebPart();
foreach (WebPart wp in WebPartManager.WebParts)
{
if (wp.GetType() == typeof(ListViewWebPart))
{
webPart = (ListViewWebPart)wp;
}
}
return webPart;
}
private void ApplyStrategySecurity(string camlFilter)
{
// Get the listview webpart
ListViewWebPart wp = GetListViewWebPart();
// Apply the query to the listview
XmlDocument doc = new XmlDocument();
doc.LoadXml(wp.ListViewXml);
if (camlFilter.Length > 0)
{
XmlNode queryNode = doc.SelectSingleNode("//Query");
XmlNode whereNode = queryNode.SelectSingleNode("Where");
if (whereNode != null)
queryNode.RemoveChild(whereNode);
XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "Where", string.Empty);
newNode.InnerXml = camlFilter;
queryNode.AppendChild(newNode);
}
wp.ListViewXml = doc.OuterXml;
}

SharePoint: How do I create a new list from a list template?

I've created a list template based on an Issue list and it is saved in the List Template Gallery. Now how do I create a new list based on this template?
string internalName = "MyListTemplateName";
SPListTemplate t = null;
foreach (SPListTemplate template in web.ListTemplates)
{
if (template.InternalName.Equals(internalName)
{
t = template;
break;
}
}
web.Lists.Add("nameoflist", "description", t);
I just encountered the same situation today.
I saved a list as a template and i wanted to use that template in a new list.
On Sharepoint 2013, go to Site Contents > Add an App >
Scroll down and you will see a page numbering saying that you are on page 1
Click on the second page and all your saved templates will be there
It probably just took a while for the timer job to fire.
The template eventually showed up as an option under Lists > Create > Tracking section after a few minutes.
I'm surprised that Johan Leino's answer is marked as useful multiple times as it doesn't work in this particular case. If you create a template yourself, web.ListTemplates does not store it and you won't be able to create the list. It's only working for out-of-the-box templates.If you want to create a list based on your custom template you need to do it this way:
SPListTemplateCollection listTemplates = web.Site.GetCustomListTemplates(web);
SPListTemplate listTemplate = listTemplates["MyCustomTemplate"];
Guid listId = web.Lists.Add("My New List Name", "My Description", listTemplate);
if (listId != null) { //all good }

Resources