Get SharePoint list data that instantiated workflow? - c#

So I'm creating a SharePoint 2010 project in Visual Studio that contains a Sequential Workflow (Farm Solution only) item that is associated with a certain list so that when an item is added to the list the workflow starts.
My question is, say an item was added in the following format:
Name | Email
Dave | dave#dave.com
Is there a way, programatically, to store this data in variables? As is, without using any hardcoded indexes or anything, just programatically have it so that I can pull this data and store it in two C# variables (int iD, string emailAddress) and the workflow knows which list item kicked it off?
At the moment the way I'm doing it is:
using (SPWeb oWeb = SPContext.Current.Web) {
SPList oList = web["List Name"];
string name = list.Items[(oList.ItemCount - 1)]["Name"].ToString();
}
But I'd rather not use indexers as there's a chance that the index is off if another item is added rapidly and if the list is reordered then ... disaster.
Thanks in advance!

Solved it guys! Very stupid of me to overlook this, but here is the solution:
C# creates a workflowProperties variable for you of type SPWorkflowActivationProperties which contains all the methods and properties to get the data you need:
public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties(); // auto generated code.
Then to get the list item data or even list data all you do is:
SPList oList = workflowProperties.List; // get the list that contains the list item on which the workflow instance is running.
SPListItem oItem = workflowProperties.Item; // get the list item on which the workflow instance is running.
So to get my "Name" data I would've had to:
string name = workflowProperties.Item["Name"].ToString();
Hope this helps someone, although fortunately nobody is as stupid as me so you guys will probably figure it out yourself.

Related

CSOM SharePoint - Add Item to List with proper FieldValues - FieldLookUpValues

I hope somebody can help me with my issue.
My actual workflow to get add a new item to my list in SharePoint looks like this:
using (var clientContext = new ClientContext(Website))
{
List oList = clientContext.Web.Lists.GetByTitle("List");
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
//add Item
ListItem listItem = oList.AddItem(itemCreateInfo);
listItem["Effort"] = 5.0;
listItem["ItemStart"] = DateTime.Now.ToLocalTime();
listItem["Workpackage"] = "PRG";
listItem.Update();
clientContext.ExecuteQuery();
}
Pretty straight forward and it "works" except for the Item: listItem["Workpackage"] = "PRG";
If I fetch the list to show me all values in item.FieldValues I get that this is a FieldLookUpValue. I display it like so ((FieldLookupValue)value.Value).LookupValue.
My problem is, that I need to set this value to the item I add to the list because without it SharePoint doesn't recognize it in the right field.
If I use the code above I get that this field ["Workpackage"] is maybe write-protected or that I transferd the wrong data for this field. Thats why I think that I have to transfer the data as a LookUpValue, but I really don't get how to make this work.
I hope I could describe my problem as close as possible.
If you need any further information, please let me know.
And thanks for the help anyway!
Best Regards
Please have a look here: https://sharepoint.stackexchange.com/questions/73252/sharepoint-client-model-and-setting-up-lookup-field
You need to pass the ID of the item from the lookup list.

SharePoint CSOM adding invalid records

I am writing some scripting processes in C# to update a SharePoint Online list with data from a system database (there's a whole heap in between, but that's irrelevant).
The issue I am having is that when I add new records, they are being added without validation rules being applied by SharePoint e.g. you can add an entry that has a missing mandatory field which then shows up OnLine with a (!) Required placeholder.
In the example below, lets assume the list has these (text) fields all of which are tagged as mandatory.
Title
Address
Rating (Just a simple 3 letter code)
And then the script has the following to add a new item ..
ClientContext clientContext = new ClientContext("my sponline site url");
List myList = client.Web.Lists.GetByTitle("my list");
ListItemCreationInformation newRecContext = new ListItemCreationInformation();
ListItem newItem = myList.AddItem(newRecContext);
newRecord.Add("Title", "My Name");
newRecord.Add("Address", "My Address");
/* Removed for Testing validation*/
//newRecord.Add("Rating", "AAA");
newItem.Update();
clientContext.Load(newItem);
clientContext.ExecuteQuery();
If the "Rating" field is populated then all good, item gets added... but if the line is removed should it not raise an exception to say that a mandatory field (Rating) is not provided.
Why is the CSOM not checking for validity?
Edit: I am using the Microsoft.SharePointOnline.CSOM (v16.1.7115.1200) package from NuGet
None of the code object models apply those rules. They're strictly for the UI controls shown to users. This is by design.
Well, this is how Sharepoint works. If you add list items programmatically - by default it does not validate required fields. You should just keep it in mind (on other hand - you have more freedom). You can add your custom validation in place where you save item. Other way is to create list item event receiver for your list and add your validation in the ItemUpdating method.

Slow iterating thru Outlook.Items to find contacts

I am having an issue where I am trying to find at least one contact inside any of the outlook folders. I have a recursive function that iterates thru the items inside a folder and if the item is of type contact then we add it to a list. However, this code runs extremely slow when folders have a large number of records say 4000 items.
Is there any way just to get contacts or is there a way to make this code more efficient?
foreach (var item in folderBase.Items)
{
if (returnFirst && result.Count > 0)
break;
if ((item is Outlook.ContactItem))
{
result.Add((Outlook.ContactItem)item);
}
}
Firstly, storing 4000 live Outlook objects in a list is a bad idea: you will run out of RPC channels in case of online Exchange store at item 255. Store the entry ids and use them to call Namespace.GetItemFromID() when you actuqlly need it; then release it as soon as you are done.
Secondly, use MAPIFolder.GetTable - it will let you retrieve values from multiple items without actually opening them; perfect in your case. Try something like the following (VB script):
set Folder = Application.ActiveExplorer.CurrentFolder
set Table = Folder.GetTable("[MessageClass] = 'IPM.Contact' ")
Table.Columns.Add("EntryID")
while not Table.EndOfTable
set Row = Table.GetNextRow()
vEntryId = Row.Item(1)
Debug.Print vEntryId
wend
You need to use the Find/FindNext or Restrict methods of the Items class instead.
Read more about these methods and see the sample code in the following articles:
How To: Use Restrict method in Outlook to get calendar items
How To: Retrieve Outlook calendar items using Find and FindNext methods
You can use the MessageClass property of Outlook items to get the contact items only.

Quality Center - Can I get the items from dropdown box in QC?

I am using Quality Center's OTA API in C# and I need to get the items from the dropdowns in QC to a SQL database. Some lists weren't long so I did them manually but the [BG_PROJECT] list has a lot of items so I really don't want to do it manually. How would this be done?
On TDConnection object you have Customization property.
On it you have Customization Listening.
CustomizationLIsts allows access to all the lists defined in the project.
You can see exact api in documentation.
Dropdown list values are stored in the Customization.Lists object.
I don't know C# but I'd do it like this in VBA:
QCC As TDConnection
myList As CustomizationList
I As Integer
Initialise QCC object and connect to domain and project then:
Set myList = QCC.Customization.Lists.List("NameOfList")
For I = 1 To myList.RootNode.Children.Count
MsgBox(myList.RootNode.Children.Item(I).Name)
Next I
(Btw, I wouldn't really use MsgBox() to display each value of the list, I used it here to demonstrate my general approach).

Add member to marketing list in CRM 2011 plugin

I hope there is someone who can help me out here. I have found an article which gives you the short piece of code to add Add Multiple Entities (Members) to a Marketing List. So far so good. I'm coming across this problem. I have a custom lookup field which gets another marketing list (has contacts, accounts or leads) inside the marketing member list. Now I need to migrate (add) those members into my new marketing list. The code I have:
1. AddListMembersListRequest request = new AddListMembersListRequest();
2. request.ListId = Origmarketing_List_Id.Id;
3. request.MemberIds = new Guid[1];
4. request.MemberIds[0] = guid;
5. AddListMembersListResponse resp = (AddListMembersListResponse)service.Execute(request);
Line 2 is the ID I get from the EntityReference(Look Up field getting another Marketing List), now the third and fourth line I'm setting is something I'm really confused about but still I'm sure I'm going right here because I'm setting it to the listmemberid. In this example I just had one cause I wanted to try out how it works. The guid in line 4 bdw gets the right value, it is declared at the top of my code(and I have output it on another field just to check it grabs the right value). Also can someone please show how you would do this when you want to add multiple entities? Thanks. I'm registering my plugin on pre-operation(Create). And the plugin itself doesn't fire any errors, but it just doesn't seem to add any members on my new list. I would really much appreciate if somone could help me out here. Thank You Really Much in Advance.
First of all, change event to post-operation, because you don't have the GUID of created entity yet, as a matter of fact you dont have the entity itself as well that's why it's called pre-operation.
To add multiple entities try pass a GUIDs array like in code bellow:
// Setup the CrmConnection and OrganizationService instances
CrmConnectionInstance = new CrmConnection(ConfigurationConstants.CrmConnectionName);
OrgServiceInstance = new OrganizationService(CrmConnectionInstance);
// Create the marketing list
Guid NewMarketingListId = Guid.Empty;
Microsoft.Xrm.Sdk.Entity CurrentList = new Microsoft.Xrm.Sdk.Entity(MarketingListConstants.MarketingListEntityName);
CurrentList[MarketingListConstants.MarketingListTypeAttribute] = false;
CurrentList[MarketingListConstants.ListNameAttribute] = "NameOfList";
// For contacts, a value of 2 should be used.
CurrentList[MarketingListConstants.CreatedFromCodeAttribute] = new OptionSetValue(2);
// Actually create the list
NewMarketingListId = OrgServiceInstance.Create(CurrentList);
// Use the AddListMembersListRequest to add the members to the list
List<Guid> MemberListIds = new List<Guid>();
// Now you'll need to add the Guids for each member to the list
// I'm leaving that part out as adding values to a list is very basic.
AddListMembersListRequest AddMemberRequest = new AddListMembersListRequest();
AddMemberRequest.ListId = NewMarketingListId;
AddMemberRequest.MemberIds = memberIds.ToArray();
// Use AddListMembersListReponse to get information about the request execution
AddListMembersListResponse AddMemberResponse = OrgServiceInstance.Execute(AddMemberRequest) as AddListMembersListResponse;

Categories