get calendarrules by servicenameid - dynamics-crm

In Dynamics 365 (on premise) I need to delete Service Restrictions, Associated with a specific service. It looks like they are stored in "calendarrules", but when i try to find some info in my collection of calendarrules, I do not see any attributes similar to "ServiceNameId".
I found the information that this can be done simply by changing the entries of the calendar rule and updating the original calendar, so I try to do it as follows, but can't see attributes referring me to services.
I get the collection as follows:
SystemUser user = service.Retrieve(SystemUser.EntityLogicalName, UserId, new ColumnSet("calendarid")).ToEntity<SystemUser>();
Calendar userCalendar = service.Retrieve(Calendar.EntityLogicalName, user.CalendarId.Id, new ColumnSet(true)).ToEntity<Calendar>();
EntityCollection entityCollection = (EntityCollection)userCalendar.Attributes["calendarrules"];

Ok, it can be found on innercalendars and updated by this way:
SystemUser user = service.Retrieve(SystemUser.EntityLogicalName, serviceappointment.OwnerId.Id, new ColumnSet("calendarid")).ToEntity<SystemUser>();
Calendar userCalendar = service.Retrieve(Calendar.EntityLogicalName, user.CalendarId.Id, new ColumnSet(true)).ToEntity<Calendar>();
EntityCollection entityCollection = (EntityCollection)userCalendar.Attributes["calendarrules"];
foreach (Entity ent in entityCollection.Entities)
{
int num = 0;
Entity newEntity = service.Retrieve("calendar", ((EntityReference)ent.Attributes["innercalendarid"]).Id, new ColumnSet(true));
EntityCollection calendarRules = (EntityCollection)newEntity.Attributes["calendarrules"];
List<int> list = new List<int>();
foreach (CalendarRule cr in calendarRules.Entities)
{
if (cr.ServiceId != null)
{
list.Add(num);
}
num++;
}
for (int i = 0; i < list.Count; i++)
{
calendarRules.Entities.Remove(calendarRules.Entities[list[i]]);
}
newEntity.Attributes["calendarrules"] = calendarRules;
service.Update(newEntity);
}
Perhaps there is a simpler way, but for now this is enough

Related

Calling 'Read' when the data reader is closed is not a valid operation error using Entity Framework database first approach

I am creating a Web API that will fetch information from a table using Entity Framework database-first approach using stored procedures. ListAllTeams_Result is the complex type object created in Entity Framework. I am looping through the import function GetAllTeams() and populating the complex type. I am getting an error in my business layer when trying to access the data access layer
The error that I am getting is the following code
var team = _teamRepository.GetAllTeams();
The result of the query cannot be enumerated more than once.
Note: this error is in the inner stack and doesn't stop the application from executing
foreach (var t in team)
Calling 'Read' when the data reader is closed is not a valid operation.
Note : This stops execution
Business Layer
public IEnumerable<TeamDto> GetTeam()
{
var team = _teamRepository.GetAllTeams();
if (team != null)
{
foreach (var t in team.ToList())
{
yield return Mapper.Map<TeamDto>(t);
}
}
yield break;
}
DataAccess layer:
public IEnumerable<ListAllTeams_Result> GetAllTeams()
{
using (var mcrContext = new MCREntities())
{
return (from team in mcrContext.ListAllTeams("")
select new ListAllTeams_Result
{
TeamID = team.TeamID,
TeamDescription = team.TeamDescription,
CountryCode = team.CountryCode,
CreatedBy = team.CreatedBy,
CreatedDate = team.CreatedDate,
ModifiedBy = team.ModifiedBy,
ModifiedDate = team.ModifiedDate
});
}
}
I have found what the problem is. I had to add ToList in the return
using (var mcrContext = new MCREntities())
{
return (from team in mcrContext.ListAllTeams("")
select new ListAllTeams_Result
{
TeamID = team.TeamID,
TeamName = team.TeamName,
TeamDescription = team.TeamDescription,
CountryCode = team.CountryCode,
CreatedBy = team.CreatedBy,
CreatedDate = team.CreatedDate,
ModifiedBy = team.ModifiedBy,
ModifiedDate = team.ModifiedDate
}).ToList();
}

Master details batch crud with entity framework only saving single detail record

I'm building a master details page with batch editing, that is multiple details records with single master record. but only one detail record is being saved into the data base. I tried to debug & found that detail loop is executing multiple time accurately but not the saving multiple data. Here is my Code for save method:
public ActionResult CMN_VAL_FORM(HRM_CMN_VLU_MST_ViewModel model)
{
//var ctx=new Entities1();
var CMN_VLU_MST_OBJ = new HRM_CMN_VLU_MST();
var CMN_VLU_DTL_OBJ = new HRM_CMN_VLU_DTL();
//using (TransactionScope transaction = new TransactionScope())
//{
using (var ctx = new Entities1())
{
var type_code = ctx.ExecuteStoreQuery<string>("select get_pk_code('hrm_cmn_vlu_mst','CMN_VLU_TYPE_CODE') from dual").SingleOrDefault(); //A scalar function to generate the code in the format yymmdd0001
var value_code = ctx.ExecuteStoreQuery<string>("select get_pk_code('hrm_cmn_vlu_dtl','CMN_VLU_CODE') from dual").SingleOrDefault();
CMN_VLU_MST_OBJ.CMN_VLU_TYPE_CODE = type_code;
CMN_VLU_MST_OBJ.CMN_VLU_REM = model.CMN_VLU_REM;
CMN_VLU_MST_OBJ.CMN_VLU_TYPE_FOR = model.CMN_VLU_TYPE_FOR;
CMN_VLU_MST_OBJ.CMN_VLU_TYPE_SRTNM = model.CMN_VLU_TYPE_SRTNM;
CMN_VLU_MST_OBJ.ENTRY_DATE = DateTime.Now;
CMN_VLU_MST_OBJ.CMN_VLU_TYPE_NAME = model.CMN_VLU_TYPE_NAME;
foreach (var item in model.HRM_CMN_VLU_DTL)
{
CMN_VLU_DTL_OBJ.CMN_VLU_LEVL = item.CMN_VAL_LEVL;
CMN_VLU_DTL_OBJ.CMN_VLU_REM = item.CMN_VLU_REM;
CMN_VLU_DTL_OBJ.CMN_VLU_SLNO = item.CMN_VLU_SLNO;
CMN_VLU_DTL_OBJ.CMN_VLU_TITL = item.CMN_VAL_TITL;
CMN_VLU_DTL_OBJ.CMN_VLU_CNTN = item.CMN_VAL_CNTN;
CMN_VLU_DTL_OBJ.CMN_VLU_CODE = value_code;
CMN_VLU_DTL_OBJ.CMN_VLU_TYPE_CODE = type_code;
CMN_VLU_DTL_OBJ.ENTRY_DATE = DateTime.Now;
CMN_VLU_DTL_OBJ.MAIL_ADDR_INT = item.MAIL_ADDR_INT;
CMN_VLU_DTL_OBJ.MAIL_ADDR_EXT = item.MAIL_ADDR_EXT;
CMN_VLU_DTL_OBJ.MAIL_AUTO_SEND_INT = item.MAIL_AUTO_SEND_INT;
CMN_VLU_DTL_OBJ.MAIL_AUTO_SEND_EXT = item.MAIL_AUTO_SEND_EXT;
CMN_VLU_DTL_OBJ.ACTIVE_STATUS = item.ACTIVE_STATUS;
CMN_VLU_MST_OBJ.HRM_CMN_VLU_DTL.Add(CMN_VLU_DTL_OBJ);
ctx.SaveChanges();
var temp_value_code = Int32.Parse(value_code);
temp_value_code++;
value_code = temp_value_code.ToString();
ctx.HRM_CMN_VLU_MST.AddObject(CMN_VLU_MST_OBJ);
}
ctx.SaveChanges();
// transaction.Complete();
//}
}
return View();
}
No Error message for the code, but not saving multiple detail records. What I'm doing wrong?
What I was doing wrong, I created the object just once & updated that same object every time while executing the foreach loop! I just moved the object declaration into the foreach loop & it works like a charm!

Dynamically choose which properties to get using Linq

I have an MVC application with a dynamic table on one of the pages, which the users defines how many columns the table has, the columns order and where to get the data from for each field.
I have written some very bad code in order to keep it dynamic and now I would like it to be more efficient.
My problem is that I don't know how to define the columns I should get back into my IEnumerable on runtime. My main issue is that I don't know how many columns I might have.
I have a reference to a class which gets the field's text. I also have a dictionary of each field's order with the exact property It should get the data from.
My code should look something like that:
var docsRes3 = from d in docs
select new[]
{
for (int i=0; i<numOfCols; i++)
{
gen.getFieldText(d, res.FieldSourceDic[i]);
}
};
where:
docs = List from which I would like to get only specific fields
res.FieldSourceDic = Dictionary in which the key is the order of the column and the value is the property
gen.getFieldText = The function which gets the entity and the property and returns the value
Obviously, it doesn't work.
I also tried
StringBuilder fieldsSB = new StringBuilder();
for (int i = 0; i < numOfCols; i++)
{
string field = "d." + res.FieldSourceDic[i] + ".ToString()";
if (!string.IsNullOrEmpty(fieldsSB.ToString()))
{
fieldsSB.Append(",");
}
fieldsSB.Append(field);
}
var docsRes2 = from d in docs
select new[] { fieldsSB.ToString() };
It also didn't work.
The only thing that worked for me so far was:
List<string[]> docsRes = new List<string[]>();
foreach (NewOriginDocumentManagment d in docs)
{
string[] row = new string[numOfCols];
for (int i = 0; i < numOfCols; i++)
{
row[i] = gen.getFieldText(d, res.FieldSourceDic[i]);
}
docsRes.Add(row);
}
Any idea how can I pass the linq the list of fields and it'll cut the needed data out of it efficiently?
Thanks, Hoe I was clear about what I need....
Try following:
var docsRes3 = from d in docs
select (
from k in res.FieldSourceDic.Keys.Take(numOfCols)
select gen.getFieldText(d, res.FieldSourceDic[k]));
I got my answer with some help from the following link:
http://www.codeproject.com/Questions/141367/Dynamic-Columns-from-List-using-LINQ
First I created a string array of all properties:
//Creats a string of all properties as defined in the XML
//Columns order must be started at 0. No skips are allowed
StringBuilder fieldsSB = new StringBuilder();
for (int i = 0; i < numOfCols; i++)
{
string field = res.FieldSourceDic[i];
if (!string.IsNullOrEmpty(fieldsSB.ToString()))
{
fieldsSB.Append(",");
}
fieldsSB.Append(field);
}
var cols = fieldsSB.ToString().Split(',');
//Gets the data for each row dynamically
var docsRes = docs.Select(d => GetProps(d, cols));
than I created the GetProps function, which is using my own function as described in the question:
private static dynamic GetProps(object d, IEnumerable<string> props)
{
if (d == null)
{
return null;
}
DynamicGridGenerator gen = new DynamicGridGenerator();
List<string> res = new List<string>();
foreach (var p in props)
{
res.Add(gen.getFieldText(d, p));
}
return res;
}

Error trying to loop through Linq query results

I need to pull any amount of records that correspond to a specific value (CourseCode), and insert these records into another table. This code works fine as long as the Linq code returns only one record, however if there is any more than that I get the following message:
An object with the same key already exists in the ObjectStateManager.
The existing object is in the Modified state. An object can only be
added to the ObjectStateManager again if it is in the added.
Below is my code:
if (_db == null) _db = new AgentResourcesEntities();
var prodCodes = from records in _db.CourseToProduct
where records.CourseCode == course.CourseCode
select records;
foreach (var pt in prodCodes.ToList())
{
agentProdTraining.SymNumber = symNumber;
agentProdTraining.CourseCode = course.CourseCode;
agentProdTraining.ProductCode = pt.ProductCode;
agentProdTraining.DateTaken = course.DateTaken;
agentProdTraining.Method = course.Method;
agentProdTraining.LastChangeOperator = requestor;
agentProdTraining.LastChangeDate = DateTime.Now;
agentProdTraining.DateExpired = course.ExpirationDate;
agentProdTraining.ProductCode = pt.ProductCode;
agentProdTraining.NoteId = pt.NoteId;
_db.AgentProductTraining.AddObject(agentProdTraining);
_db.SaveChanges();
PtAdded++;
EventLog.WriteEntry(sSource, "Product Training added", EventLogEntryType.Warning);
}
The loop is re-adding the same agentProdTraining object even though property values are changed. Create a new instance for each loop execution.
foreach (var pt in prodCodes.ToList())
{
var agentProdTraining = new AgentProductTraining();
agentProdTraining.SymNumber = symNumber;
agentProdTraining.CourseCode = course.CourseCode;
agentProdTraining.ProductCode = pt.ProductCode;
agentProdTraining.DateTaken = course.DateTaken;
agentProdTraining.Method = course.Method;
agentProdTraining.LastChangeOperator = requestor;
agentProdTraining.LastChangeDate = DateTime.Now;
agentProdTraining.DateExpired = course.ExpirationDate;
agentProdTraining.ProductCode = pt.ProductCode;
agentProdTraining.NoteId = pt.NoteId;
_db.AgentProductTraining.AddObject(agentProdTraining);
_db.SaveChanges();
PtAdded++;
EventLog.WriteEntry(sSource, "Product Training added", EventLogEntryType.Warning);
}

How to read extended properties from Outlook contacts using EWS

I'm currently attempting to read certain properties from Outlook Contact objects through Microsoft's EWS managed API. I retrieve these Contact objects from the FindItems() function. Some of these fields are extended properties such as the Title or User1 field and I'm having difficulty reading them. At the moment, I have:
Guid propertySetId = new Guid("{00062004-0000-0000-C000-000000000046}");
ExtendedPropertyDefinition titleProp = new ExtendedPropertyDefinition(propertySetId, 0x3A45, MapiPropertyType.String);
ExtendedPropertyDefinition user1Prop = new ExtendedPropertyDefinition(propertySetId, 0x804F, MapiPropertyType.String);
string title, user1;
contact.TryGetProperty(titleProp, out title);
contact.TryGetProperty(user1Prop, out user1);
When running this, TryGetProperty always returns false. I have verified that these fields are populated in Outlook for the contacts that I am searching for.
Edit: This is how I retrieve the contact objects.
ExchangeService service = //...
Mailbox userMailbox = new Mailbox(emailAddress);
FolderId folderId = new FolderId(WellKnownFolderName.Contacts, userMailbox);
FindItemsResults<Item> results;
const string AQS = "Category:~>\"CategoryTag\"";
ItemView view = new ItemView(200);
results = service.FindItems(folderId, AQS, view);
foreach (var result in results)
{
Contact contact = result as Contact;
//...Try to read fields
}
You need to change the ItemView to include the properties (PropertySet) you wish to access.
var user1Val = string.Empty;
var user1Prop = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Address, 0x804F, MapiPropertyType.String);
ExtendedPropertyDefinition[] extendedFields = new ExtendedPropertyDefinition[] { user1Prop };
PropertySet extendedPropertySet = new PropertySet(BasePropertySet.FirstClassProperties, extendedFields);
ItemView view = new ItemView(200) { PropertySet = extendedPropertySet };
// ...
var title = contact.CompleteName.Title; // Title value
contact.TryGetProperty(user1Prop, out user1Val); // user field 1 value

Resources