Create post entity to crm - c#-4.0

I am getting unexpected error while trying to create post in Dynamics CRM. Below is the code:
Entity objEntity = new Entity("post");
objEntity["regardingobjectid"] = IncidentID;
objEntity["text"] = URLs;
objEntity["source"] = new OptionSetValue(2);
objEntity["type"] = new OptionSetValue(4);
Guid newPostID = lOrgService.Create(objEntity);
Please suggest what could be the problem.

It could be a multitude of things, which is hard to pinpoint based on the small amount of code you have submitted, but my best guess is that IncidentID is a GUID, where regardingobjectid should be an EntityReference.
Try replacing:
objEntity["regardingobjectid"] = IncidentID;
with
objEntity["regardingobjectid"] = new EntityReference("incident", IncidentID);

Related

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.

Issue in web.EnsureUser for FBA users programatically

I am trying to ensure user through web.EnsureUser for FBA users. Here is my code snipplet.
as an example :
string fbUsername = "i:0#.f|ie-fbamembership|userlogin";
SPUser userSP = site.RootWeb.EnsureUser(fbUsername);
But i get an error user does not exists. But i can surely add this user to the site collection manually.
But could not add the user through code (programatically).
Any ideas? i could not find a solution for this in the net.
S.R.G
SharePoint Developer
I'm very late to this question, but I had the same problem today and a colleague of mine finally found a solution.
I just use the AllowUnsafeUpdates property of my SPWeb object and write :
web.AllowUnsafeUpdates = true;
oUser = web.EnsureUser(userId);
In my case, userId is from a PeoplePicker, I construct it like this :
PickerEntity selectedEntity = (PickerEntity)peEditor.ResolvedEntities[i];
string userId = selectedEntity.Key;
Hope it can helps someone !

How to get id of saved entity

Dynamics CRM 2011 on premise
I can barely believe I can't find this out by Googling. MSDN is useless.
Here is some C# from a plugin:
integ_creditpayment creditpayment = new integ_creditpayment();
creditpayment.integ_Amount = totalPay;
//set more properties
context.AddObject(creditpayment);
context.SaveChanges();
Now I want to get the value of the id field in integ_creditpayment.
Can I get this immediately from creditpayment.id? (As in, does context.SaveChanges() cause the creditpayment variable to be updated with the new id?)
I'm assuming your real code is more complicated, but there is no need to use the context in your example code:
integ_creditpayment creditpayment = new integ_creditpayment();
creditpayment.integ_Amount = totalPay;
//set more properties
creditpayment.Id = service.Create(creditpayment);
You can also use a type initializer and get rid of your object all together if you'd like:
Guid id = service.Create(new integ_creditpayment
{
integ_Amount = totalPay;
});
service in this case is of type IOrganizationService
After the SaveChanges() you can get the record id with:
Guid justCreatedId = creditpayment.Id;

unable to change the account reference inside the contact using sdk in crm2011

I am unable to change the client by updating the contact using crm 2011 sdk.Here is the code i am using to do that :
Entity contact = new Entity();
contact.LogicalName = "contact";
contact.Attributes = new AttributeCollection();
EntityReference clientLookup = new EntityReference();
clientLookup.Id = NewClientBId;
clientLookup.LogicalName = "account";
contact.Attributes.Add("parentcustomerid", clientLookup);
contact.Attributes.Add("contactid", workItem.Id);
SynchronousUtility.UpdateDynamicEntity(CrmConnector.Service, contact);
The code runs fine without any error but when i go to web portal and check the record ,it still points to the old account though updated the modofication time stamp.I also checked the sql profiler query which shows up as below :
exec sp_executesql N'update [ContactBase] set
[ModifiedOn]=#ModifiedOn0, [ModifiedBy]=#ModifiedBy0,
[ModifiedOnBehalfBy]=NULL where ([ContactId] =
#ContactId0)',N'#ModifiedOn0 datetime,#ModifiedBy0
uniqueidentifier,#ContactId0
uniqueidentifier',#ModifiedOn0='2013-07-04
09:21:02',#ModifiedBy0='2F8D969F-34AB-E111-9598-005056947387',#ContactId0='D80ACC4E-A185-E211-AB64-002324040068'
as can be seen above the column i have updated is not even there in the set clause of the update query.Can anyone help me with this ?
I tested your code and it works:
Entity contact = new Entity();
contact.LogicalName = "contact";
contact.Attributes = new AttributeCollection();
EntityReference clientLookup = new EntityReference();
clientLookup.Id = new Guid("3522bae7-5ae5-e211-9d27-b4b52f566dbc");
clientLookup.LogicalName = "account";
contact.Attributes.Add("parentcustomerid", clientLookup);
contact.Attributes.Add("contactid", new Guid("16dc4143-5ae5-e211-9d27-b4b52f566dbc"));
As you can see I used existing Id in my environment, and to perform the update I used
service.Update(contact);
Reasons why your code is not working:
NewClientBId is not the right account Guid
workItem.Id is not the right contact Guid
the function SynchronousUtility.UpdateDynamicEntity has errors

Displaying Managed Property in Search Results - FAST Search for Sharepoint 2010

We are working with Fast Search for Sharepoint 2010 and had some backend setup done with creating some managed properties e.g. BestBetDescription, keywords etc.
From the front-end part we are creating an application what will fetch all these properties and display in a grid.
However while querying the backend we are NOT getting these managed properties (BestBetDescription) along with other properties such as Title, URL etc.
Following is my source code:
settingsProxy = SPFarm.Local.ServiceProxies.GetValue<SearchQueryAndSiteSettingsServiceProxy>();
searchProxy = settingsProxy.ApplicationProxies.GetValue<SearchServiceApplicationProxy>("FAST Query SSA");
keywordQuery = new KeywordQuery(searchProxy);
keywordQuery.EnableFQL = true;
keywordQuery.QueryText = p;
keywordQuery.ResultsProvider = SearchProvider.FASTSearch;
keywordQuery.ResultTypes = ResultType.RelevantResults;
ResultTableCollection resultsTableCollection = keywordQuery.Execute();
ResultTable searchResultsTable = resultsTableCollection[ResultType.RelevantResults];
DataTable resultsDataTable = new DataTable();
resultsDataTable.TableName = "Results";
resultsDataTable.Load(searchResultsTable, LoadOption.OverwriteChanges);
return resultsDataTable;
The results are returned and I cannot see the Managed properties which we create in the resultDataTable.
Is there any property I missed or is this a backend issue ?
Thanks.
Hi if you are creating your custom Metadata Property then u should use this option to be selected
please check below link
http://screencast.com/t/SQdlarjhx4F
You can find this option in :
central admin:- services :- fast search :- Metadata Property :- your property
I was missing a property KeywordQuery.SelectProperties
So the code looks something like this
String[] arrSearchProperties = new String[] { "Title", "body", "url" };
KeywordQuery.SelectProperties(arrSearchProperties);
This will fetch you all the Managed Properties defined by you.

Resources