How to check whether current component is checkout as well as the user details who checkout using tridion coreservices - tridion

I want to write small function to check whether passed Item Object is checkout in Tridion or not if yes then it will return "true" and also I want to get the details of user who has checkout the Item, using Tridion 2011 core services.
I know we have TryCheckout as well as Checkout in our CoreServiceClient but it returns Identifiable Object only.

You need to look at the LockType on the item. Consider doing something like this
SessionAwareCoreService2010Client client = new SessionAwareCoreService2010Client();
ComponentData data = (ComponentData)client.Read("tcm:300-85609", new ReadOptions());
FullVersionInfo info = (FullVersionInfo)data.VersionInfo;
The full version info contains all the info you need (i.e. CheckOutUser and LockType). LockType is an Enumeration defined by Tridion.ContentManager.Data.ContentManagement.LockType, and includes the following set of flags:
None - The item is not locked.
CheckedOut - The item is checked-out. This can mean either a temporary (edit) lock, a permanent lock (explicit check-out performed by user) or a workflow lock.
Permanent - The item is permanently checked-out, that is using an explicit check-out action.
NewItem - The item is a new item, that is it has been created, but not checked-in for the first time yet.
InWorkflow - The item is in a workflow.

Below is the sample code to get the details of ItemCheckedout with Users details in it.
public static Dictionary<bool,string> ItemCheckedOutDetails(string ItemUri, CoreServiceClient client, ReadOptions readOpt, ItemType itemType)
{
Dictionary<bool, string> itemDetails = null;
FullVersionInfo itemInfo = null;
if (itemType == ItemType.Component)
{
// reading the component data
var itemData = (ComponentData)client.Read(ItemUri, readOpt);
itemInfo = (FullVersionInfo)itemData.VersionInfo;
}
else if (itemType == ItemType.Page)
{
// reading the page data
var itemData = (PageData)client.Read(ItemUri, readOpt);
itemInfo = (FullVersionInfo)itemData.VersionInfo;
}
else if (itemType == ItemType.StructureGroup)
{
// reading the structuregroup data
var itemData = (StructureGroupData)client.Read(ItemUri, readOpt);
itemInfo = (FullVersionInfo)itemData.VersionInfo;
}
else if (itemType == ItemType.Publication)
{
// reading the Publication data
var itemData = (PublicationData)client.Read(ItemUri, readOpt);
itemInfo = (FullVersionInfo)itemData.VersionInfo;
}
else if (itemType == ItemType.ComponentTemplate)
{
// reading the component template data
var itemData = (ComponentTemplateData)client.Read(ItemUri, readOpt);
itemInfo = (FullVersionInfo)itemData.VersionInfo;
}
else if (itemType == ItemType.PageTemplate)
{
// reading the Page template data
var itemData = (PageTemplateData)client.Read(ItemUri, readOpt);
itemInfo = (FullVersionInfo)itemData.VersionInfo;
}
else if (itemType == ItemType.MultimediaType)
{
// reading the Multimedia Type data
var itemData = (MultimediaTypeData)client.Read(ItemUri, readOpt);
itemInfo = (FullVersionInfo)itemData.VersionInfo;
}
if (itemInfo != null)
{
if (itemInfo.LockType.Value == LockType.CheckedOut)
{
itemDetails.Add(true, itemInfo.CheckOutUser.Title);
}
}
return itemDetails;
}

Related

View not update after POST although View Model updated

I am writing ASP.NET Custom Component, I want to update my view with following code below:
#if (Model.TableDatasource != null){
//Write some thing to html page example: tables or span
}
At the first, Model.TableDatasource is null, user choose information and Controller Review View(Model) like this:
var model = new PrintDeliveryTicketModel()
{
PlantList = CommonHelper.GetPlantList(),
};
if (request == null)
{
return View(model);
}
else
{
var currentPlant = JsonConvert.DeserializeObject<PlantList>(request);
var FullModel = new PrintDeliveryTicketModel()
{
PlantList = CommonHelper.GetPlantList(),
CurrentPlant = JsonConvert.DeserializeObject<PlantList>(request),
DriverList = JsonConvert.DeserializeObject<List<Drivers>>(CommonHelper.GetDriver(currentPlant.CodePlant, currentPlant.PlantNo.Value).Content.ReadAsStringAsync().Result),
CustomerList = JsonConvert.DeserializeObject<List<Customer>>(CommonHelper.GetCustomer().Content.ReadAsStringAsync().Result),
RecipeList = JsonConvert.DeserializeObject<List<Recipe>>(CommonHelper.GetRecipe(currentPlant.CodePlant, currentPlant.PlantNo.Value).Content.ReadAsStringAsync().Result),
SitesList = JsonConvert.DeserializeObject<List<Site>>(CommonHelper.GetSite().Content.ReadAsStringAsync().Result),
// TruckList = JsonConvert.DeserializeObject<List<Truck>>(CommonHelper.GetTruck().Content.ReadAsStringAsync().Result)
};
return View(FullModel);
}
At the Debug time, I notice that break-point stop at return View(FullMode) and Break-point at ViewFile(cshtml) has value. but nothing printed at view.
Hope some help.

In Skype bot framework Attachments Content null

I'm trying to access the list of attachments sent by the user to the skype bot that I'm developing.
Here is how I access the attachment details ,
public async Task<HttpResponseMessage> Post([FromBody]Activity message)
{
if (message.Attachments != null)
{
if (message.Attachments.Count > 0)
{
List<Attachment> attachmentList = message.Attachments.ToList();
foreach (var item in attachmentList)
{
var name = item.Name;
var content = item.Content;
}
}
}
}
But I get null for the following even though the attachment count is greater than zero,
var name = item.Name;
var content = item.Content;
Am I doing this right?
Maybe do something like this...
List<Attachment> attachmentList = message?.Attachments?.Where(x => x != null)?.ToList() ?? new List<Attachment>();
This would hopefully always set attachmentList to an empty list or a list containing non null items?

If statement in Controller not saving changes

I have an if statement in my Controller which decides whether a checkbox is checked or not.
It works fine in the if statement and changes the properties, but when i go to send it back to the view these changes aren't saved.
Controller
public ActionResult GetUserRights(string userLogin)
{
if (userLogin != null)
{
Manager manager = new Manager();
var userlogin = manager.GetUserData(userLogin);
var userrights = userlogin.RightsId.Select(s => new { id = s, text = s });
var rightdetails = manager.GetAllRightsRows();
var rightsDetails = from r in rightdetails
orderby r.Id
select new RightDetail
{
RightID = r.Id,
RightDescription = r.Description,
RightName = r.Name,
ParentID = r.ParentId,
TypeColor = r.TypeColor,
Value = r.Value,
Checked = false
};
foreach (var userright in userrights)
{
foreach (var rightdets in rightsDetails)
{
if(rightdets.RightID == userright.id)
{
rightdets.Checked = true;
break;
}
}
}
return View("_RightsTreeListPartial", rightsDetails); <==== ALL CHECKED
PROPERTIES ARE false EVEN THOUGH SOME ARE BEING CHANGED IN THE IF STATEMENT.
}
return View("Index");
}
Let me know if you need any more info.
Thanks
With an IEnumerable, I am not sure of the reason why, but you cannot edit an item using an if statement so the code below is correct and does what it is supposed to, however as it is IEnumerable non of the changes are saved, also the process below is very heavy and long winded for what we need to do.
Original Code
foreach (var userright in userrights)
{
foreach (var rightdets in rightsDetails)
{
if(rightdets.RightID == userright.id)
{
rightdets.Checked = true;
break;
}
}
}
The new code takes a lot less time and will therefore improve the wait time. Firstly the IEnumerable is converted to a List, then, using a for-loop, the data is iterated through until a match is found, then within an if statement the item is changed (using original code and just converting from IEnumerable to List should work but I wouldn't recommend using it).
New Code
var rightdetail = rightsDetails.ToList();
foreach (var userright in userrights)
{
for (var i = 0; i < rightdetail.Count(); i++)
{
if (rightdetail[i].RightID == userright.id)
{
rightdetail[i].Checked = true;
break;
}
}
}

Maintain DropdownList Selected value in mvc

I have a DropDown and on selcted indexchanged it forcefully postback and Binds a table,but after postback it didn't maintain the state.
my view is
#Html.DropDownListFor(m=>m.fkSubMenuID, (IEnumerable<SelectListItem>)ViewBag.list,"Select"
,new { id = "ddlSubMenu",onchange="SelectedIndexChanged()" })
and my controller is
public ActionResult ChildMenuOfSubMenu()
{
if (Session["DDlId"] == null || Convert.ToInt32(Session["DDlId"]) == 0)
{
UlrikenEntities dc = new UlrikenEntities();
var query = (from m in dc.ulriken_tblChildMenu
join sb in dc.ulriken_tblSubMenu on m.fkSubMenuID equals sb.pkSubMenuID
where m.Status == true && sb.fkMainMenuID == 1
select m).ToList();
Ulriken.Models.ChildMenu ObjHomeEvents = new Models.ChildMenu();
ObjHomeEvents.FormDetails = query;
FillDeptName();
Session["DDlId"] = null;
return View(ObjHomeEvents);
}
else
{
Int64 id = Convert.ToInt64(Session["DDlId"]);
UlrikenEntities dc = new UlrikenEntities();
var query = (from m in dc.ulriken_tblChildMenu
join sb in dc.ulriken_tblSubMenu on m.fkSubMenuID equals sb.pkSubMenuID
where m.Status == true && m.fkSubMenuID == id && sb.fkMainMenuID==1
select m).ToList();
Ulriken.Models.ChildMenu ObjHomeEvents = new Models.ChildMenu();
ObjHomeEvents.FormDetails = query;
FillDeptName();
//string ddlValue= ViewData.TemplateInfo.GetFullHtmlFieldId("ddlSubMenu");
Session["DDlId"] = null;
return View(ObjHomeEvents);
}
//return View();
}
and my javascript function is :
function SelectedIndexChanged() {
document.demoForm.submit();
}
Somebody guide me where am i doing wrong
Your controller action has no parameters... You need at least one parameter in the controller action to retrieve the value selected by the user.
public ActionResult ChildMenuOfSubMenu(int fkSubMenuID)
{
// ....
}
Probably will be better to have a method to show the view when the request is an HTTP GET and another one to handle the form submit (HTTP POST):
public ActionResult ChildMenuOfSubMenu()
{
// This method gets called in a HTTP GET
}
[HttpPost]
public ActionResult ChildMenuOfSubMenu(int fkSubMenuID)
{
// This one gets called when user performs the submit to the form
}

Update database from viewmodel but add new records too

I have a view that has a list of items (which can be added to dynamically via jQuery).
When I POST the viewmodel back to the controller, if the code can't find the ID, how do I insert new items and save them to the database.
My initial code is below - the updates are saved, but the new items aren't saved:
//
// POST: /Objective/Edit/model
[HttpPost]
public ActionResult Edit(ObjectivesEdit model)
{
if (model.Objectives != null)
{
foreach (var item in model.Objectives)
{
// find the database row
Objective objective = db.objectives.Find(item.ID);
if (objective != null)
{
// Set the database row to the posted values
objective.objective = item.objective;
objective.score = item.score;
objective.possscore = item.possscore;
}
else // database item not found, so add a new item
{
// add a new objective
// This doesn't seem to add/save a new record
Objective obj = new Objective();
obj.objective = item.objective;
obj.score = item.score;
obj.possscore = item.possscore;
}
}
// Save the changes to the database
db.SaveChanges();
}
return View(model);
}
Thanks for any help,
Mark
You don't add the newly created objective to your context.
else // database item not found, so add a new item
{
// add a new objective
// This doesn't seem to add/save a new record
Objective obj = new Objective();
obj.objective = item.objective;
obj.score = item.score;
obj.possscore = item.possscore;
// Missing line.
db.objectives.Add(obj);
}
if you're using EF 4.0 (i.e. db is of type ObjectContext), you should use the db.AddObject(obj).
Update based on your comment:
One way is to retrieve all added items after saving changes. Another way is to modify your model when creating a new objective. Changed parts are marked with *:
foreach (var item in model.Objectives.ToList()) // *:Notice the ToList()
{
// find the database row
Objective objective = db.objectives.Find(item.ID);
if (objective != null)
{
// Set the database row to the posted values
objective.objective = item.objective;
objective.score = item.score;
objective.possscore = item.possscore;
}
else // database item not found, so add a new item
{
// add a new objective
// This doesn't seem to add/save a new record
Objective obj = new Objective();
obj.objective = item.objective;
obj.score = item.score;
obj.possscore = item.possscore;
db.AddObject(obj)
// Save the changes to the database
db.SaveChanges(); // *: save in loop to get thee ID.
item.ID = obj.ID; // *: assign the ID to the model.
}
}
return View(model);

Resources