How do I upload multiple images in specific folder in sitecore programmatically? - sitecore8

I am trying to upload large amount of images from file system into sitecore, inside a specific folder in media library. I am using Sitecore 8.1.3 version.
I have tried using MediaCreator.CreateFromFile(), MediaCreator.CreateItem(), MediaCreator.CreateFromStream() functions. Issue I am facing is while uploading images, I want all images in "XYZ" folder inside media library but instead "XYZ" folder is getting created again and again for each individual image.
so for lets say 200 images, 200 folders are getting created and inside each folder one image is getting uploaded.
it seems all three functions create the entire item path regardless of whether it already exists in media library or not.
here is how i am using it
MediaCreator creator = new MediaCreator();
fileLocation = C:\Images\temp.png
var options = new MediaCreatorOptions();
options.Destination = "/sitecore/media library/XYZ/temp";
Item newItem = creator.CreateFromFile(fileLocation, options);
it repeats for multiple images with different image names.

Can't comment. But Richard Seal'answer on StackExchange is pretty much what you are looking for I think. : https://sitecore.stackexchange.com/questions/15433/how-to-copy-multiple-items-to-a-new-location
You have 2 ways - if you can connect up Sitecore Rocks, that has an option to multi-select items and move them around in Visual Studio and rename etc...
The second option would be to use Sitecore PowerShell Extensions (SPE) to script that out. This example will give you a good starting place:
$rootOfitemsToMove = Get-Item “/sitecore/content/Location A/subfolder 4”;
$destinationItemPath = “/sitecore/content/Location B/subfolder 4”;
$templateNameToMatch = “My Template”;
Write-Host “Moving items from: ” $rootOfitemsToMove.Paths.FullPath ” to: ” $destinationItem.Paths.FullPath ” …”;
Get-ChildItem | Where-Object { $_.TemplateName -match $templateNameToMatch } | ForEach-Object {
$name = $_.Name
if(![string]::IsNullOrEmpty($name))
{
$newName = "$($name)0" # Add some logic here to rename the folder
$newPath = "$($destinationItemPath)/$($newName)"
Move-Item -Path $_.ItemPath -Destination $newPath;
Write-Host “Item moved to: $($newPath)“;
}
else
{
Write-Host “Couldn’t move Item: ” $name;
}
}
Write-Host “Moving items complete.”;
You would need to work out the logic of how the folders are renamed. Moving a parent folder should also move the child items with it.

Related

Search for folders that doesn't contain file with a wanted name

I'm working on organizing my movies folders (1500+) and making sure that they all have subtitles in English and Arabic. All my subtitle files have the language code in them (filename.ar.srt) or (filename.en.srt). I want to write a powershell or any other convenient method to list folders that doesn't contain [*.XX.srt] which I can substitute with the language code I'm searching for.
Try executing this in the directory containing all your movies' folders :
$language = Read-Host "Language to search for"
foreach ($folder in (dir -Directory)) {
if (-not (Get-ChildItem -Path "$($folder.Name)/*.$language.srt")) {
"Missing $language subtitle for $($folder.Name)"
}
}

Sitecore Remove broken links through broken link URL not working

To Remove broken links from sitecore I have used below URL:
http://localhost/sitecore/admin/RemoveBrokenLinks.aspx
This has removed some of the links. But still scan for broken links window is showing some broken links.What should I do to remove all that broken links.
You can try to use Sitecore Powershell Extensions:
$item = Get-Item -Path "master:/sitecore/content"
$linkDb = [Sitecore.Globals]::LinkDatabase
$links = $linkDb.GetReferrers($item)
foreach($link in $links) {
$linkedItem = Get-Item -Path master:\ -ID $link.SourceItemID
$itemField = $linkedItem.Fields[$link.SourceFieldID]
$field = [Sitecore.Data.Fields.FieldTypeManager]::GetField($itemField)
$linkedItem.Editing.BeginEdit()
$field.RemoveLink($link)
$linkedItem.Editing.EndEdit()
}

In Sitecore how do I update a large number of placeholder settings that are using dynamic placeholders

For some reason the placeholder key for a large number of pages changed I need to change it back. The problem is we use dynamic place holders so parts of the placeholder key are random GUIDs and they are in every sublayouts placeholder key
So I need
/bacontent/../baproducttabscaffold{6e0244e2-3583-47cb-b575-0bc920186d42}
to be
/bacontent/../baproducttabscaffold{895ecd6f-4abb-478d-9dad-88e5e05b8c30}
What would be the most efficient way of changing these?
What I ended up doing is using Sitecore Powershell Extensions
https://marketplace.sitecore.net/en/Modules/Sitecore_PowerShell_console.aspx
The script I used
$pages = gci -Path "master:\my path" -Recurse
$oldGuid = "6e0244e2-3583-47cb-b575-0bc920186d42"
$newGuid = "895ecd6f-4abb-478d-9dad-88e5e05b8c30"
foreach($page in $pages){
$oldRenderings = $page.__Renderings
$newRenderings = $oldRenderings.Replace($oldGuid, $newGuid)
$page.BeginEdit()
$page.__Renderings = $newRenderings
[void]$page.EndEdit()
}

Sitecore 8: Change of datasource template for a sublayout

I have a doubt regarding the change of datasource template of a sublayout. Now, there are two sublayouts: Sub1 and Sub2 which are having Template1 as their datasource template. Until the time I discovered that i need a different datasource template for Sub1, i had already created many items of sublayouts Sub1 and Sub2.
Template2 will now replace Template1 as datasource template for sublayout Sub1.
Now, i have to change the template of all those items that were created with sublayout as Sub1.
The problem is I have to manually change each item's template via content editor--> Configure-->Change Template technique, which is very cumbersome. Is there any other way to change the template of all those items at once ?
I suggest you to install Sitecore PowerShell Extensions and to change the template using the Sitecore PowerShell Console.
$master = [Sitecore.Configuration.Factory]::GetDatabase("master");
$entryTemplate = $master.Templates["your path to template Sub2"];
cd master:\Content\Home\Sub1FolderItems\;
//path to sub1 folder items
Get-ChildItem -recurse | ForEach-Object { if ($_.TemplateName -eq "Sub1") { $_.ChangeTemplate($entryTemplate) } };
There is another way - if you have Sitecore Rocks installed, you can multi-select all the items, right click and select Change Template - no code, and pretty quick unless your content items are in many different places.
As #SitecoreClimber suggested, the best approach to do this is to install Sitecore PowerShell Extensions and fix this with PowerShell. If you don't have admin access or you are not allowed to install PowerShell extensions to your machine, you can use the following .NET code to achieve what you want. Just replace the values of ID variables with the IDs of your templates and sublayouts:
// replace with the first template's ID
ID template1ID = new ID("{A0F73C76-DD4D-4037-90D4-48B616397F5D}");
// replace with the second template's ID
ID template2ID = new ID("{43A1EBB0-CABB-4682-9F5B-7765D7FB0E29}");
// replace with your sublayout's ID
ID sublayout2ID = new ID("{1C6094FA-4539-48E4-A24A-104787641A88}");
Database masterDatabase = Factory.GetDatabase("master");
TemplateItem template2Item = masterDatabase.GetTemplate(template2ID);
// Set to your RootItem
Item rootItem = masterDatabase.GetItem("{756B23C8-1C0F-41AC-9273-B18FDA047925}");
using (new SecurityDisabler())
{
foreach (Item child in rootItem.Axes.GetDescendants())
{
RenderingReference[] renderings = child.Visualization.GetRenderings(Sitecore.Context.Device, true);
IEnumerable<RenderingReference> sublayout2Renderings =
renderings.Where(x => x.RenderingID == sublayout2ID);
foreach (RenderingReference rendering in sublayout2Renderings)
{
if (!string.IsNullOrEmpty(rendering.Settings.DataSource))
{
Item datasourceItem = masterDatabase.GetItem(rendering.Settings.DataSource);
if (datasourceItem != null)
{
if (datasourceItem.TemplateID == template1ID)
{
datasourceItem.ChangeTemplate(template2Item);
}
}
}
}
}
}

sitecore query or tool to find broken items / templates (no longer exists)

Looking for a quick and easy to query my entire sitecore database (master, web, or pub) and determine where the template for an item no longer exists.
I am trying to serialize my entire tree and I am finding that there are alot of items who's templates have been removed and would like to get a list of them without too much of a headache.
When viewing these items it says template: template no longer exists, here is the code that renders this
private static void RenderQuickInfoTemplate(HtmlTextWriter output, Sitecore.Data.Items.Item item)
{
Sitecore.Data.Items.Item item2;
Sitecore.Diagnostics.Assert.ArgumentNotNull(output, "output");
Sitecore.Diagnostics.Assert.ArgumentNotNull(item, "item");
output.Write("<tr><td>");
output.Write(Translate.Text("Template:"));
output.Write("</td><td>");
using (new SecurityDisabler())
{
item2 = item.Database.GetItem(item.TemplateID);
}
bool flag = (item2 != null) && (CommandManager.QueryState("shell:edittemplate", item) == CommandState.Enabled);
if (flag)
{
output.Write("<a href=\"#\" onclick=\"javascript:scForm.postRequest('','','','shell:edittemplate');return false\">");
}
if (item2 != null)
{
output.Write(item2.Paths.Path);
}
else
{
output.Write(Translate.Text("[template no longer exists]"));
}
Thanks
i think items which base on deleted templates don't inherit from (standard) base template anymore. in quickinfo you should see that their template-ID is something like {000-0000-0000...}
you could try find all the items based on this id.
Two options that come to mind:
Check the Broken Links report
You can right-click on the left 'rail' in the content editor and choose "Broken Links" to show all items that have broken links.
Using the popular powershell module, try
Get-ChildItem -Path "master:\sitecore" -Recurse | ForEach-Object {if (!$_.Template){ write-host $_.ItemPath }}
For me that returns a whole lot of items that I can't open in the content editor.
In case it's just a few items and you want to do everything manually, you can use the dbbrowser:
<yoursiteurl>/sitecore/admin/dbbrowser.aspx.
In case you want to delete these items by script (when there are too many)
$myItemArray = New-Object System.Collections.ArrayList
Get-ChildItem -Path "master:\sitecore\content" -Recurse | ForEach-Object {if (!$_.Template){
$myItemArray.Add($_)
}}
#Show list of Items with invalid template
$myItemArray | Format-Table -Auto -Property Templateid, ItemPath
#Show count of affected items
$myItemArray.Count
$myItemArray #| Remove-Item -recurse
To actually delete the items, remove the '#' in the final line at your own risk. The script takes quite some time to run, and you might need to change the path in case you have broken items outside of content.