SharePoint CSOM adding invalid records - c#

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.

Related

Salesforce: create Opportunity Line Items along with Opportunity from C#

using Salesforce's enterprise wsdl I am trying to save opportunity line items along with opportunity. But I am getting following error:
INVALID_FIELD: No such column 'OpportunityLineItems' on entity 'Opportunity' If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
Here is my code to create line items:
if (oppLineItems.Count > 0)
{
sfOpportunity.OpportunityLineItems = new QueryResult();
sfOpportunity.HasOpportunityLineItem = true;
sfOpportunity.OpportunityLineItems.records = oppLineItems.Values.ToArray();
Pricebook2 priceBook = new Pricebook2();
priceBook.PricebookEntries = new QueryResult();
priceBook.PricebookEntries.records = new List<PricebookEntry>() { priceBookEntry }.ToArray();
sfOpportunity.Pricebook2 = priceBook;
}
oppLineItems is a dictionary whole values have proxy objects of opportunity line items.
sfOpportunity is proxy object of Opportunity which is then sent to Salesforce.
There's a very similar question here, not sure if we should mark it as duplicate though: Salesforce: Creating OpportunityLineItems as part of the Opportunity in PHP
OpportunityLineItems on Opportunity isn't a real field. Its something called "relationship name"... Similar to table alias in normal databases, useful especially when you're making joins. And HasOpportunityLineItem is a readonly field :) And I don't think these should be QueryResult, check http://www.salesforce.com/us/developer/docs/api/Content/sample_create_call.htm for some hints?
You will need to insert the Opportunity first, the operation result will give you the record's Id. Then you should insert a list (array) of the line items.
This means 2 API calls and extra considerations what to do when the Opp header saves OK but one or more lines fails... So maybe it's good idea to write an Apex webservice like I suggested in that other question.

Get SharePoint list data that instantiated workflow?

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.

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;

Set "Target Audiences" field with multiple values on a List Item

I am attempting to set the "Target Audiences" field on a list item programmatically. I have been able to set the value programmatically for one audience, but when I attempt to use multiple audiences, SharePoint tries to interpret the value I am setting as a single audience, rather than multiple. I am setting the value using the code below.
listItem[listItem.Fields["Target Audiences"].InternalName] = "Audience One";
I use this code to specify multiple audiences like so:
listItem[listItem.Fields["Target Audiences"].InternalName] = "Audience One; Audience Two";
When I do this, SharePoint tries to interpret the entire string as a single audience, and I get a message when I edit the list item that says "No exact match was found."
Am I using the correct format for specifying multiple audiences for this field, or is there a class that I should be using similar to SPFieldLookupValue?
I don't know how to save multiple audiences in a SPListItem, but if I had this problem, I'd try to print out the value of this field from a PowerShell script. Something like:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
$site = Microsoft.SharePoint.SPSite("http://yourserver");
$web = $site.openweb();
$list = $web.lists["YourList"];
$item = $list.getitembyid(itemid);
write-output $item["Target Audiences"];
EDIT: found some information about what the Audience field value actually is: http://dotnetmafia.sys-con.com/node/1181567/mobile
Just realized I never came back and answered this.
I ended up storing the names of the audiences in a column attached to each list item, then querying the object model to see if the current user is a member of those audiences. This worked for me because I was pulling the data from the list item in a custom web part, and the user never saw the actual list item.

Categories