Search This Blog

Total Pageviews

Saturday, May 28, 2011

Cancel the Workflow Through Object Model

                    using (SPSite osite = new SPSite(SPContext.Current.Site.ID))
                        using (SPWeb oweb = osite.OpenWeb(SPContext.Current.Web.ID))

                            SPList oList= oweb.Lists["List Name"];
           SPListItem olstitem = oTravelRequestList.GetItemById(133);
                            SPWorkflowManager manager = osite.WorkflowManager;

                            foreach (SPWorkflow itemWorkflow in manager.GetItemActiveWorkflows(olstitem))
                                MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:txtNotes", NamespaceManager).SetValue("Title is" + manager.GetItemActiveWorkflows(olstitem).Count);
                                oweb.AllowUnsafeUpdates = true;
                                itemWorkflow.CreateHistoryEvent((int)SPWorkflowHistoryEventType.WorkflowCancelled, 0, oweb.CurrentUser , "Cancelled by the User", "Cancelled By Siva", null);
                                oweb.AllowUnsafeUpdates = false;

                                if (!itemWorkflow.IsLocked)
                                    foreach (SPWorkflowTask taskWorkflow in itemWorkflow.Tasks)

                                        taskWorkflow["Status"] = "Canceled";
                                        oweb.AllowUnsafeUpdates = true;
                                        oweb.AllowUnsafeUpdates = false;
                                    oweb.AllowUnsafeUpdates = true;
                                    oweb.AllowUnsafeUpdates = false;


Wednesday, May 25, 2011

How to Hide InfoPath Control Programmatically c#

To hide the InfoPath control we can simply create a variable and format the control accordingly.
If you want to hide the control programmatically, set the variable value. then InfoPath will render the control accordingly.
XPathNodeIterator NodeIterator = nav.Select("/my:myFields/my:MainSection/my:BidDocument", NamespaceManager);
Above code will display the control.
Following code will hide the control
nav.SelectSingleNode("/my:myFields/my:isPicVisible", NamespaceManager).SetValue("False");

Tuesday, May 24, 2011

Programmatically Setting Taxonomy Field Values

static void Main(string[] args)
    var url = ConfigurationManager.AppSettings["Url"].ToString();
    var library = ConfigurationManager.AppSettings["Library"].ToString();

    // Reads XML, considering
    // (key => lookup field,value => taxonomy field)
    Dictionary<string, string> mappingFields = XMLHelper.ReadDictionaryXML("MappingFields.xml");

    using (SPSite site = new SPSite(url))
        using (SPWeb web = site.RootWeb)
            // Gets the list/library
            SPList list = web.Lists[library];

            foreach (SPListItem item in list.Items)
                // Iterates through all mapped fields
                foreach (var mappedField in mappingFields)
                    if (item.Fields.ContainsField(mappedField.Key))
                        // Allows updates without a trace
                        web.Site.AllowUnsafeUpdates = true;

                        // Gets the lookup field instance
                        var lookupValueList = (item[mappedField.Key] as SPFieldLookupValueCollection).ToList();

                        // Gets the taxonomy field instance
                        TaxonomyField managedField = item[mappedField.Value] as TaxonomyField;

                        // Gets the current taxonomy session
                        TaxonomySession session = new TaxonomySession(web.Site, false);

                        // Gets the term store (by SspId)
                        var termStoreCol = session.TermStores[managedField.SspId];

                        // Gets the terms of a specific term set (by TermSetId)
                        var termCol = termStoreCol.GetTermSet(managedField.TermSetId).Terms;

                        var listTerms = new List<Term>();

                        // Iterates through the lookup values
                        foreach (var itemValue in lookupValueList)
                            string value = itemValue.LookupValue;

                            // Gets the correspondent term for the each value
                            // found in the lookup values list
                            var termToSet = termCol[value];


                        // Sets the field value using the list of terms
                        managedField.SetFieldValue(item, listTerms);

                        // Persists the item

                        // Denies further unsafe updates
                        web.Site.AllowUnsafeUpdates = false;

Monday, May 23, 2011

In a meeting workspace get all agenda items from a list programmatically.

using Microsoft.SharePoint.Meetings;
Sample code:
using(SPSite site = new SPSite(<enter your workspace url>))
using (SPWeb web = site.OpenWeb())
    SPMeeting meeting = SPMeeting.GetMeetingInformation(web);

    SPQuery query = new SPQuery();
    query.MeetingInstanceId = (int)SPMeeting.SpecialInstance.AllButSeries;
    query.Query = @"<Query>
                             <FieldRef Name='ID' />

    SPList list = web.Lists[<enter your list>];
    foreach (SPListItem item in list.GetItems(query))

Export a SharePoint Designer workflow to Visual Studio in SharePoint 2010

Common situation when designing workflow is that you start to design workflow in SharePoint Designer and then you want to add some code in Visual Studio.
In the following example, I will go through the procedure to create a workflow in SharePoint Designer and then open it and deploy it from Visual Studio.
Let’s start by creating a reusable workflow in SharePoint Designer
Enter the workflow information
and then just implement one step to set the document status to approve:
Next, in order to be able to reuse it in all the sites from site collection in SharePoint, publish the workflow globally:
Save the workflow and then Save it as a template :
You should see the confirmation :
Now, if you go to the Site Library, you will see this:
Save the .wsp localy and then, open Visual Studio and choose to import a workflow:
You can deploy it as a sandbox:
and select your .wsp and all the elements included in the .wsp:
Wait for importation to be completed:
Then deploy the solution:
If you have already saved it in SharePoint Designer, you will have a conflict and see this:
Just select Resolve Automatically to delete the workflow deployed by SharePoint Designer.
From the website, you should now see that the feature corresponding to this workflow is activated:
Then add the workflow on a document library:
Then, start a workflow on a document: