Programmatically removing sublayouts in Sitecore - c#

Does anyone know how to remove the renderings from a Sitecore item?
I want to remove all of the sublayouts so I can replace them with a new set.
I have tried this but it does not seem to work. Nothing changes on the item.
I appear to be able to get the renderings like this:
RenderingReference[] renderings = item.Visualization.GetRenderings(Sitecore.Context.Device, true);
But there appears to be no way to then set them.
I can also get the renderings like this (from the link above):
LayoutDefinition layoutDefinition = LayoutDefinition.Parse(LayoutField.GetFieldValue(item.Fields[Sitecore.FieldIDs.LayoutField]));
DeviceDefinition device = layoutDefinition.GetDevice(Sitecore.Context.Device.ID.ToString());
if (device.Layout != null) device.Layout = null;
if (device.Renderings != null) device.Renderings = new ArrayList();
But again this does not work. Clearing the device from the layoutDefinition and setting the modified one has led to this exception: No connection could be made because the target machine actively refused it. And I now cannot view the item at all!
I feel like I'm barking up the wrong tree, any ideas?
Using Sitecore 6.4
UPDATE Re: techphoria414
Code I tried:
layoutDefinition.Devices.Clear();
layoutDefinition.Devices.Add(device);

I think your exception is unrelated. To actually save your changes, you need to edit the item. Be sure you always access and update the value throgh LayoutField.Value.
LayoutField layoutField = new LayoutField(item.Fields[Sitecore.FieldIDs.LayoutField]);
LayoutDefinition layout = LayoutDefinition.Parse(layoutField.Value);
//make your changes to the LayoutDefinition here
item.Editing.BeginEdit();
layoutField.Value = layout.ToXml();
item.Editing.EndEdit();

Related

How can I get the the Microsoft Access Database Application Title via C#

I need to get the Application Title from an access database via C#. I have seen some samples using Office VBA: I.E: https://learn.microsoft.com/en-us/office/vba/api/access.application.apptitle
The issue is that I don't have a reference to a class library to be able to access the Application.AppTitle property. I have tried a few references, most notably:
using Microsoft.Office.Interop.Access.Dao
But I can't access the Application.AppTitle via any of the properties. For example:
var dbe = new Microsoft.Office.Interop.Access.Dao.DBEngine();
var db = dbe.OpenDatabase(#"c:\\Sample.mdb");
// Show database properties
// db.Properties. "When I expand this there is no AppTitle"
Does anyone have any other approach that has worked for them to access the MS Access AppTitle via C#?
Thanks in advance!
Ian
The AppTitle property doesn't show up until you set it in Access or via code (and with RefreshTitleBar
Your code works because you're looping to check for the name. You won't find the property if it's empty.
You can use the loop method like you do above or check for it directly using database.properties("AppTitle") - just make sure you trap for an error in case it's empty
Update: I figured it out. This was different than I expected, but the solution was:
var dbEngine = new DBEngine();
var database = dbEngine.OpenDatabase(#"c:\\Sample.mdb");
Microsoft.Office.Interop.Access.Dao.Property allowBypassKeyProperty = null;
foreach (Microsoft.Office.Interop.Access.Dao.Property property in database.Properties)
{
if (property != null)
{
if (property.Name == "AppTitle")
{
MessageBox.Show(property.Name.ToString());
MessageBox.Show(property.Value.ToString());
}
}
}

Clearing a product from nopCommerce cache programatically

I have run into a problem where I need to manually click the clear cache button for a specific product to appear correct on a specific page after it's saved.
I'm wondering how I can go about this with code, but only clear the cache for that product.
clearing complete cache can be done like this:
var _cacheManager = EngineContext.Current.Resolve<ICacheManager>();
_cacheManager.Clear();
I have tried doing it like this but it didn't work.
var product = _productService.GetProductById(productModel.Id);
var productTemplateCacheKey = string.Format(ModelCacheEventConsumer.PRODUCT_TEMPLATE_MODEL_KEY,
productModel.Id);
_cacheManager.Remove(productTemplateCacheKey);
Not sure how to go about this, anyone got any ideas?
Thanks

Find out the source control state of a ProjectItem from a Visual Studio extension

I'm currently developing a Visual Studio extension. For a new feature I need to find out whether a given ProjectItem (file) was modified (has "Pending Changes" since the last commit). For this I would like to query the source control provider.
I already tried searching all the properties of the ProjectItem but there is nothing hidden in there.
I also tried getting a service associated with source control from the Package.cs. I tried getting IVsSccManager2, IVsSccManager3 and IVsSccGlyphs. All return null (the test project is under source control). edit: I try to get these services by calling GetService(typeof(IVsSccManager2)) inside my Package.cs. The source control plugin of my debugging session correctly shows the changed files at the time this is called.
I can't seem to find anything online about this topic. How can I find out the modified state? Is it even possible?
After letting this topic sit for some time I recently came back to it and found the solution thanks to the help of my collegue. The problem was that I had no experience in bitwise comparison so I didn't know how to handle the response properly. Luckily my collegue gave me the right tip.
To interpret the result of status (thanks to #simon-mourier for the help on this code):
uint[] sccStatus = new uint[] { 0 };
if (VSConstants.S_OK == manager.GetSccGlyph(1, new[] { filePath }, new[] { VsStateIcon.STATEICON_NOSTATEICON }, sccStatus))
{
__SccStatus status = (__SccStatus)sccStatus[0];
}
One has to do bitwise comparison with the state of __SccStatus you are looking for, for example:
if ((sccStatus[0] & (uint)__SccStatus.SCC_STATUS_RESERVED_2) != 0)
return true;
The comparison returns true in case the state is set. If you need help on what specific state combinations can mean, just comment here and I can help on that.

How do I listen for TimeSlider Event in ArcMap using C#

I am updating rows of featureclasses via a plugin using C# and ArcObjects. For some reason the featureclasses then are not refreshing properly when the slider is moved, they do display properly if I manually refresh the map, however. I want to test if forcing a full refresh of the display on timeslider updates will work around the issue. In order to do that I want to listen for timeslider update events in my code.
I have seen another bug related to ArcSDE Direct Connection tables not displaying properly, but this is not my issue as I am not using an ArcSDE Direct Connection.
I have also recomputed attribute indexes (on time field) and spatial indexes but no dice.
So, hoping that refreshing on timeslider updates might help.
My C# plugin is running in ArcMap 10.1 SP1. Background enterprise geoDB is on SQLServer.
Thanks!
After a day of searching I posted my question, then found my solution within an hour.
ITimeDisplayEvents_DisplayTimeChangedEventHandler DTC_EH;
private void enableTimeDisplayEventHandler(bool enable = true)
{
IMxDocument pMxDoc = ArcMap.Document;
IMap pMap = pMxDoc.FocusMap;
IActiveView pActiveView = pMap as IActiveView;
IScreenDisplay pScreenDisplay = pActiveView.ScreenDisplay;
ITimeDisplay pTimeDisplay = pScreenDisplay as ITimeDisplay;
DTC_EH = new ITimeDisplayEvents_DisplayTimeChangedEventHandler(this.OnDisplayTimeChangedEventHandler);
((ITimeDisplayEvents_Event)pTimeDisplay).DisplayTimeChanged += DTC_EH;
}
private void OnDisplayTimeChangedEventHandler(IDisplay d, object oldvalue, object newvalue)
{
IMxDocument pMxDoc = ArcMap.Document;
IMap pMap = pMxDoc.FocusMap;
IActiveView pActiveView = pMap as IActiveView;
pActiveView.Refresh();
}
Hopefully somebody else finds that useful.

Saving WorkItem#IterationPath on newly created Iteration

I can successfully create an iteration path via:
var commonservice = collection.GetService<ICommonStructureService>();
// create new area path and iteration path
var iterationRoot = commonservice.GetNodeFromPath("\\MyTeamProject\\Iteration");
var newIterationPath = commonservice.CreateNode("my new sprint", iterationRoot.Uri);
However, when I try and assign this path to a work item and save it the field doesn't validate.
If I run the tests again (with the iteration already created) the same code succeeds.
Does anybody know how to make this work?
This fixed it for me:
WorkItemStore wis = (WorkItemStore)tfsTeamProjColl.GetService(typeof(WorkItemStore));
wis.RefreshCache();
wis.SyncToCache();
Maybe it will help someone.
I experienced exactly the same behavior, and unfortunately #JWC answer didn't help. The solution which works for me can be found by this link.
So, this is a quick summary in case the original answer gets lost.
The key point is to use WorkItemServer class. It lives in the Microsoft.TeamFoundation.WorkItemTracking.Proxy.dll assembly.
First of all, you create a WorkItemStore instance:
var store = collection.GetService<WorkItemStore>();
Then, create necessary iteration paths:
var commonservice = collection.GetService<ICommonStructureService>();
var iterationRoot = commonservice.GetNodeFromPath("\\MyTeamProject\\Iteration");
var newIterationPath = commonservice.CreateNode("my sprint", iterationRoot.Uri);
Next, refresh the cache in TFS (I suspect this is similar to pressing F5 in web interface):
var wiServer = collection.GetService<WorkItemServer>();
wiServer.SyncExternalStructures(WorkItemServer.NewRequestId(), commonservice.GetProjectFromName("MyTeamProject").Uri);
store.RefreshCache();
And finally, assign newly created work item to the newly created iteration:
var wi = new WorkItem(store.Projects["MyTeamProject"].WorkItemTypes["Product Backlog Item"]);
wi.Title = "Hello from API";
wi.Description = "This work item was created from API";
wi.Fields["Assigned To"].Value = "Yan Sklyarenko";
wi.IterationPath = FormatPath(commonservice.GetNode(newIterationPath).Path, "Iteration", "MyTeamProject");
wi.Save();
That's it! The method FormatPath translates the iteration path to the form required by the work item IterationPath field, that is from \MyTeamProject\Iteration\my sprint to MyTeamProject\my sprint.
Hope this can save some time.
NOTE: I run this towards TFS 2013.
You are likely running into a caching issue. Try clearing the cache after you create the iteration. A couple things you could try:
Get a new copy of the WorkItemStore.
Disconnect and reconnect to TFS
Check to see if there's a "refresh" method on either the WIS or on the TFS server objects. I've shut down my dev instance of TFS for the night, and I don't recall if there's anything like it.
If that's not quite it, post your code and I'll see if I can reproduce it.
I had the similar issue. I created Areapath and then created a query where the AreaPath was used. I did call store.RefreshCashe() but it did not work. Only in Debugger when I run store.RefreshCashe() two times manually.
Thanks "Yan Sklyarenko". I tried your Suggestion and it works fine (TFS Server 2012).

Categories