Search This Blog

Total Pageviews

Saturday, May 28, 2011

Cancel the Workflow Through Object Model



 SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    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;
                                        taskWorkflow.SystemUpdate();
                                        oweb.AllowUnsafeUpdates = false;
                                    }
                                   
                                    oweb.AllowUnsafeUpdates = true;
                                    SPWorkflowManager.CancelWorkflow(itemWorkflow);
                                    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.
image
image
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);
nav.SelectSingleNode("/my:myFields/my:isPicVisible",NamespaceManager).SetValue("True");
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

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.taxonomy.taxonomyfield.aspx



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];

                            listTerms.Add(termToSet);
                        }

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

                        // Persists the item
                        item.SystemUpdate();

                        // 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>
                       <Where>
                          <IsNotNull>
                             <FieldRef Name='ID' />
                          </IsNotNull>
                       </Where>
                    </Query>";

    SPList list = web.Lists[<enter your list>];
    foreach (SPListItem item in list.GetItems(query))
    {
        Console.WriteLine(item[item.Fields.GetFieldByInternalName("Title").Id]);
    }
}

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
scl4
Enter the workflow information
scl5
and then just implement one step to set the document status to approve:
scl6
Next, in order to be able to reuse it in all the sites from site collection in SharePoint, publish the workflow globally:
scl17
Save the workflow and then Save it as a template :
scl8
You should see the confirmation :
scl7
Now, if you go to the Site Library, you will see this:
scl9
Save the .wsp localy and then, open Visual Studio and choose to import a workflow:
scl10
You can deploy it as a sandbox:
scl11
and select your .wsp and all the elements included in the .wsp:
image
Wait for importation to be completed:
scl12
Then deploy the solution:
scl13
If you have already saved it in SharePoint Designer, you will have a conflict and see this:
image
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:
scl14
Then add the workflow on a document library:
scl16
Then, start a workflow on a document:
scl18