Delete build definitions in TFS via API - api

When creating TFS build definitions via the API, I need to first delete, if the definition pre-exists:
if (BuildServer.QueryBuildDefinitions(teamProject).Any(d => d.Name == buildDefinitionName))
{
buildDefinition = BuildServer.GetBuildDefinition(teamProject, buildDefinitionName);
var builds = BuildServer.QueryBuilds(buildDefinition);
if (builds != null && builds.Any())
{
Console.WriteLine("delete {0} builds for build definition: {1}", builds.Count(), buildDefinition.Name);
BuildServer.DeleteBuilds(builds);
}
if (buildDefinition.Workspace.Mappings.Any())
{
var mappings = buildDefinition.Workspace.Mappings.Select(m => m.ServerItem).ToArray();
foreach (var mapping in mappings)
{
Console.WriteLine("remove workspace mapping: {0}", mapping);
buildDefinition.Workspace.RemoveMapping(mapping);
}
}
Console.WriteLine("delete build definition: {0}", buildDefinition.Name);
BuildServer.DeleteBuildDefinitions(new[] { buildDefinition });
}
This works as does the subsequent:
buildDefinition = BuildServer.CreateBuildDefinition(teamProject);
buildDefinition.Name = buildDefinitionName;
However, when the first build gets run, it throws an error about conflicting workspaces:
Exception Message: Unable to create the workspace 'some-new-workspace' due to a mapping conflict. You may need to manually delete an old workspace. You can get a list of workspaces on a computer with the command 'tf workspaces /computer:%COMPUTERNAME%'.
Details: The path C:\some-path is already mapped in workspace some-old-workspace. (type MappingConflictException)
As you can see in the first snippet, my attempt to delete workspaces with .Workspace.RemoveMapping(), has no effect. The workspaces still exist on the build controller. I can delete them manually but they really should get deleted when I delete the build definition. Is there some other DeleteWorkspace() mechanism in the API?
A more complete code gist is here: https://gist.github.com/grenade/cce374cb4e27e366bc5b

It turns out that the reason it's complicated is that the owner of the various workspaces created by the build could be some other user (that the build agent runs under).
I found a way to do it by relying on the previous build definition id which is used in the workspace naming convention [build definition id]_[build agent id]_[workspace host]:
var workspaceNamePrefix = string.Concat(buildDefinition.Id, '_');
var workSpaces = VersionControlServer.QueryWorkspaces(null, null, null).Where(w => w.Name.StartsWith(workspaceNamePrefix)).ToArray();
for (var i = workSpaces.Count() - 1; i > -1; i--)
{
try
{
workSpaces[i].Delete();
Console.WriteLine("delete workspace: {0}", workSpaces[i].Name);
}
catch (ResourceAccessException rae)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(rae.Message);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("workspace needs to be deleted by an administrator using the following command:");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("tf workspace /delete {0};{1}", workSpaces[i].Name, workSpaces[i].OwnerName);
Console.ResetColor();
}
}
I have updated the gist: https://gist.github.com/grenade/cce374cb4e27e366bc5b

Related

Project Doesn't Build After Installed Sitecore TDS

After setting up Sitecore TDS, my project will not build. I'm new to Visual Studio and also new to working with Sitecore. It seems that it cannot find a particular setting, but a Google search is not coming up with anything:
Severity Code Description Project Path File Line Suppression State
Error The "AnalyzeProject" task failed unexpectedly.
System.MissingFieldException: Field not found: 'HedgehogDevelopment.SitecoreProject.Tasks.SitecoreDeployInfo.ParsedItem'.
at HedgehogDevelopment.SitecoreProject.Analysis.TemplateStructure.Validate(Dictionary`2 projectItems, XDocument scprojDocument)
at HedgehogDevelopment.SitecoreProject.Tasks.ProjectAnalysis.AnalysisEngine.<>c__DisplayClass4_1.<GetReport>b__0()
at HedgehogDevelopment.SitecoreProject.Tasks.ProjectAnalysis.ExecutionTimer.Time(Action action)
at HedgehogDevelopment.SitecoreProject.Tasks.ProjectAnalysis.AnalysisEngine.GetReport(Dictionary`2 projectItems, XDocument scprojDocument)
at HedgehogDevelopment.SitecoreProject.Tasks.AnalyzeProject.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() B2B.Core C:\Program Files (x86)\MSBuild\HedgehogDevelopment\SitecoreProject\v9.0 C:\Program Files (x86)\MSBuild\HedgehogDevelopment\SitecoreProject\v9.0\HedgehogDevelopment.SitecoreProject.targets 144
Apparently my project does still build and will run, but that error pops up each time regardless.
This can happen when you have TDS validations enabled and are missing some DLLs.
In this directory:
C:\Program Files (x86)\MSBuild\HedgehogDevelopment\SitecoreProject\v9.0\
Add the following DLLs:
Microsoft.Web.Infrastructure.dll
TDSWebDeploy.Services.Contracts.dll
If you have TDS installed, you may be able to source those DLLs from somewhere in your C:\Program Files (x86)\MSBuild\HedgehogDevelopment directory. If not, someone else on your team may have them.
You can also try disabling and enabling validations by right clicking your TDS project --> Properties --> Validations tab.
I have seen this issue on numerous occasions across numerous dev boxes. We have opened support tickets regarding it, but no conclusions were drawn.
The specific error is that the HedgehogDevelopment.SitecoreProject.Tasks.SitecoreDeployInfo.ParsedItem field is missing on an object. The fact that ParsedItem is mentioned implies that some form of parsing may be occurring, and that it didn't work as expected. This is conjecture, but if parsing is occurring, it would be worth ensuring that your serialized item files are accessible to the user/group that is performing this parsing.
Here is the code that is failing:
public override IEnumerable<Problem> Validate(Dictionary<Guid, SitecoreDeployInfo> projectItems, XDocument scprojDocument)
{
List<Problem> problems = new List<Problem>();
foreach (KeyValuePair<Guid, SitecoreDeployInfo> projectItem in projectItems)
{
// ** Presumably, it's failing here **
string item = projectItem.Value.ParsedItem.Properties["template"];
if (string.IsNullOrEmpty(item))
{
continue;
}
Guid guid = new Guid(item);
if (guid == TemplateStructure.TEMPLATE)
{
problems.AddRange(this.ValidateTemplate(projectItems, projectItem.Value.Item));
}
else if (guid != TemplateStructure.TEMPLATE_SECTION)
{
if (guid != TemplateStructure.TEMPLATE_FIELD)
{
continue;
}
problems.AddRange(this.ValidateField(projectItems, projectItem.Value.Item));
}
else
{
problems.AddRange(this.ValidateSection(projectItems, projectItem.Value.Item));
}
}
problems.RemoveAll((Problem r) => r == null);
return problems;
}
Here is the definition for SitecoreDeployInfo:
using HedgehogDevelopment.SitecoreCommon.Data.Items;
using System;
namespace HedgehogDevelopment.SitecoreProject.Tasks
{
public class SitecoreDeployInfo
{
public IItem ParsedItem;
public SitecoreItem Item;
public SitecoreDeployInfo()
{
}
}
}

Get list of Workspace which are already checked in

I am using TFS 2012. So after I checkin a changes of a workspace to the server I don’t need that workspace any more in my server. So how it can be deleted logically? The entries from the database need to be deleted for that workspace?
How can I get the list of workspace whose changes are already checked in ? I think there will be sql some script by using database and tables of that collection. I tried to identify from workspace table entries. But didn’t find any identification there. So please help.
This little program should work for you:
class Program
{
static private TfsTeamProjectCollection _tfs;
static void Main(string[] args)
{
_tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("myuri"));
var service = _tfs.GetService<VersionControlServer>();
Workspace[] ws = service.QueryWorkspaces(null, null, null);
foreach(Workspace s in ws)
{
var pend = s.GetPendingChanges();
if (pend.Count() == 0)
{
Console.WriteLine("Workspace {0} has no pending changes", s.Name);
// s.Delete()
continue;
}
}
// Console.ReadLine();
}
}
I believe that uncommenting the Delete will clear them for you, but that is untested, so use with caution.

Check if a file exists in the project in WinRT

I have a WinRT Metro project which displays images based on a selected item. However, some of the images selected will not exist. What I want to be able to do is trap the case where they don't exist and display an alternative.
Here is my code so far:
internal string GetMyImage(string imageDescription)
{
string myImage = string.Format("Assets/MyImages/{0}.jpg", imageDescription.Replace(" ", ""));
// Need to check here if the above asset actually exists
return myImage;
}
Example calls:
GetMyImage("First Picture");
GetMyImage("Second Picture");
So Assets/MyImages/SecondPicture.jpg exists, but Assets/MyImages/FirstPicture.jpg does not.
At first I thought of using the WinRT equivalent of File.Exists(), but there doesn't appear to be one. Without having to go to the extent of trying to open the file and catching an error, can I simply check if either the file exists, or the file exists in the project?
You could use GetFilesAsync from here to enumerate the existing files. This seems to make sense considering you have multiple files which might not exist.
Gets a list of all files in the current folder and its sub-folders. Files are filtered and sorted based on the specified CommonFileQuery.
var folder = await StorageFolder.GetFolderFromPathAsync("Assets/MyImages/");
var files = await folder.GetFilesAsync(CommonFileQuery.OrderByName);
var file = files.FirstOrDefault(x => x.Name == "fileName");
if (file != null)
{
//do stuff
}
Edit:
As #Filip Skakun pointed out, the resource manager has a resource mapping on which you can call ContainsKey which has the benefit of checking for qualified resources as well (i.e. localized, scaled etc).
Edit 2:
Windows 8.1 introduced a new method for getting files and folders:
var result = await ApplicationData.Current.LocalFolder.TryGetItemAsync("fileName") as IStorageFile;
if (result != null)
//file exists
else
//file doesn't exist
There's two ways you can handle it.
1) Catch the FileNotFoundException when trying to get the file:
Windows.Storage.StorageFolder installedLocation =
Windows.ApplicationModel.Package.Current.InstalledLocation;
try
{
// Don't forget to decorate your method or event with async when using await
var file = await installedLocation.GetFileAsync(fileName);
// Exception wasn't raised, therefore the file exists
System.Diagnostics.Debug.WriteLine("We have the file!");
}
catch (System.IO.FileNotFoundException fileNotFoundEx)
{
System.Diagnostics.Debug.WriteLine("File doesn't exist. Use default.");
}
catch (Exception ex)
{
// Handle unknown error
}
2) as mydogisbox recommends, using LINQ. Although the method I tested is slightly different:
Windows.Storage.StorageFolder installedLocation =
Windows.ApplicationModel.Package.Current.InstalledLocation;
var files = await installedLocation.GetFilesAsync(CommonFileQuery.OrderByName);
var file = files.FirstOrDefault(x => x.Name == fileName);
if (file != null)
{
System.Diagnostics.Debug.WriteLine("We have the file!");
}
else
{
System.Diagnostics.Debug.WriteLine("No File. Use default.");
}
BitmapImage has an ImageFailed event that fires if the image can't be loaded. This would let you try to load the original image, and then react if it's not there.
Of course, this requires that you instantiate the BitmapImage yourself, rather than just build the Uri.
Sample checking for resource availability for c++ /cx (tested with Windows Phone 8.1):
std::wstring resPath = L"Img/my.bmp";
std::wstring resKey = L"Files/" + resPath;
bool exists = Windows::ApplicationModel::Resources::Core::ResourceManager::Current->MainResourceMap->HasKey(ref new Platform::String(resKey.c_str()));

Eclipse plugin project preference saving exception

I am currently developing an eclipse plugin, i created my own property page which will be displayed when right-clicking on the context menu of selected project. I tried to save values of that page by pressing OK button on that page, but i got exception saying: "Exception occurred while saving project preferences: /test/.settings/com.example.plugin.prefs.Resource is out of sync with the file system: '/test/.settings/com.example.plugin.prefs'."
Following is what i implemented:
//Get the project
IAdaptable resource = getElement();
if (resource != null) {
IProject project = (IProject) resource.getAdapter(IProject.class);
}
//Define project scope
IScopeContext projectScope = new ProjectScope(project);
//Get node by qualified name
Preferences projectNode = projectScope.getNode(MyPlugin.PLUGIN_ID);
//set the value and save it
if (projectNode != null) {
projectNode.put(PROPERTIES_SERVICENAME, serviceName);
}
try {
projectNode.flush(); // Exception occurs here!!!
} catch (BackingStoreException e) {
e.printStackTrace();
}
To my knowledge, it should automatically save a file like "com.example.plugin.prefs" under runtime-EclipseApplication\test.settings as well, is that correct?
Anybody has idea how to solve this problem?

Unable to delete SharePoint 2010 ContentType "Contenty type in use."

I have tried all the recommendations on the web, to no avail.
I wrote a console application per these instructions: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontenttypecollection.delete.aspx
The "Usages.Count" is = 0. Yet, when it tries to delete the Content Type I get an Exception:
"The content type is in use."
This is a brand new (development) install. I created a test site in SP Designer, created a Content Type,then a list. Then, I removed the list, removed it from Recycle Bin and tried to remove the content type...... Ugh.
I was frustrated by this issue until I found your comment. Excellent advice.
Delete from site recycle bin.
Delete from Site Collection > Site Settings > Site Collection Administration > Recycle Bin.
Delete from End User Recycle Bin Items.
Delete from "Deleted From End User Recycle Bin."
That's a lot of recycling! Once complete, I was able to delete the content type.
In addition to the recycling bins there's also the page called "Manage files which have no checked in version" under "Permissions and Management" on document libraries - the files in there can also prevent deletion of a content type.
this powershell script form this post also worked for me
$siteURL = "The Site url"
$contentType = "Content type Name"
$web = Get-SPWeb $siteURL
$ct = $web.ContentTypes[$contentType]
if ($ct) {
$ctusage = [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($ct)
foreach ($ctuse in $ctusage) {
$list = $web.GetList($ctuse.Url)
$contentTypeCollection = $list.ContentTypes;
$contentTypeCollection.Delete($contentTypeCollection[$contentType].Id);
Write-host "Deleted $contentType content type from $ctuse.Url"
}
$ct.Delete()
Write-host "Deleted $contentType from site."
} else { Write-host "Nothing to delete." }
$web.Dispose()
using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
namespace Test
{
class ConsoleApp
{
static void Main(string[] args)
{
using (SPSite siteCollection = new SPSite("http://localhost"))
{
using (SPWeb webSite = siteCollection.OpenWeb())
{
// Get the obsolete content type.
SPContentType obsolete = webSite.ContentTypes["Test"];
// We have a content type.
if (obsolete != null)
{
IList usages = SPContentTypeUsage.GetUsages(obsolete);
// It is in use.
if (usages.Count > 0)
{
Console.WriteLine("The content type is in use in the following locations:");
foreach (SPContentTypeUsage usage in usages)
Console.WriteLine(usage.Url);
}
// The content type is not in use.
else
{
// Delete it.
Console.WriteLine("Deleting content type {0}...", obsolete.Name);
webSite.ContentTypes.Delete(obsolete.Id);
}
}
// No content type found.
else
{
Console.WriteLine("The content type does not exist in this site collection.");
}
}
}
Console.Write("\nPress ENTER to continue...");
Console.ReadLine();
}
}
}
Create a Console Application with the above code and run that project. This code will tell you the libraries in which the content types are attached. Then simply go that libraries and delete the attached content types. Then finally delete the content type from Site Actions -> Site Settings -> Site Content Types or you may use the above code as well to delete the content type.
This worked for me hope it may also work for you !!!
Thanks.