Author: sandeeppote

Sitecore MediaFolder VS Media.FileFolder

Sitecore has 2 settings in the config named MediaFolder and Media.FileFolder, both has a different functionality.

MediaFolder – path used in this setting, Sitecore shall watch this folder for the files that need to be automatically uploaded to Sitecore media library. The path in this setting can be a Site path (e.g. /folder/) or a absolute path(e.g. C:\folder). The file path should be different from Media.FileFolder. By default it is set to /upload. So any files moved to this folder will be watched by Sitecore and uploaded media library.

Disable file watcher
There is a security aspect here, if you want to only enable the file upload using Sitecore client then disable the file upload watcher.

To ensure that only way to upload file is through Media Library disable the file watcher by removing the following line in web.config in

[code language=”xml”]

[/code]

Media.FileFolder – path used in the settings can be a Site path (e.g. /folder/) or a absolute path(e.g. C:\folder)

When media files are uploaded from Media Library as a file, the files are been uploaded into configured path in this settings. So this files are not saved in database instead reside in as a physical file on server or computer.

MediaAsFile

Sitecore media items vs media files

In Sitecore there is a difference between media item and media file.
A media item can be a image, a document or a video.
Media files are kept in a computer. Its when the file is uploaded to Sitecore a media item is created where the file is associated to the media item.

Media item can be –
1. Images
2. Videos
3. Documents including PDF
4. Audio files

The media item can be managed from the Media Library.

Sitecore LiveMode.config

Sitecore Live Mode allows to run the site on content database i.e. master db rather then a web database or published content. This is useful for developers who tend to test the code without publishing Sitecore items every time it is changes in dev or local machine.

Live mode can be set in 2 ways-

  • Directly changing in Sitecore.config or config where Site settings are patched. Set your site database element value to master.
  •  Set the LiveMode.config in /App_Config/Include folder

To setup the Sitecore.config, update following-

[code language=”xml”]
<sitecore>
<sites>
<site name="yoursitename" databasename="master" …></site>
</sites>
</sitecore>
[/code]

To setup LiveMode.config, update following-

[code language=”xml”]
<sitecore>
<sites>
<site name="yoursitename">
<patch:attribute name="database">master</patch:attribute>
<patch:attribute name="filterItems">true</patch:attribute>
<patch:attribute name="enableWorkflow">true</patch:attribute>
</site>
</sites>
</sitecore>
[/code]

database attribute to master for you site.

filteritems is set to true which means that items which has publishing restrictions will be filtered out. This is recommended to be true.

enableWorkflow – set this to false if the items that are in workflow and if you want to be seen in live mode.

 

nuget restore: Exception has been thrown by the target of an invocation

I am working on Sitecore 8.2 Helix based architecture. whilst doing a build received error –
MSBuild auto-detection: using msbuild version ‘14.0’ from ‘C:\Program Files (x86)\MSBuild\14.0\bin’
Error parsing solution file… Exception has been thrown by the target of an invocation.

Was able to resolve this issue suppose while merging changes the solution was missing the End Project

Project(…)
EndProject
Project(…)

Project(…)
EndProject

After adding the “EndProject”  was able to parse the solution and build.

C# 7 Local Functions

C# 6 Local functions are useful to create helper methods and was written as below-

 
public class LocalFunctions
{
    public int Age { get; set; }

    public string C6PrivateFunction()
    {
       return $"C#6 ==> Person is {GetEligibility(Age)}";
    }        
}

private string GetEligibility(int age)
{
   return age > 18 ? "Adult" : "Youth";
}

With C# 7.0 the same can be written as below.

Inside local functions, variables and parameters from the enclosing scope are also available and the same can also be written using as expression body.

        
public string C7LocalFunction()
{
   return $"C#7 ==> Person is {GenerateEligibility(Age)}";

   string GenerateEligibility(int age)
   {
      return age > 18 ? "Adult" : "Youth";
   }
}

public string C7LocalFunction1()
{
   return $"C#7 ==> Person is {GenerateEligibility(Age)}";

   string GenerateEligibility(int age) =>  age > 18 ? "Adult" : "Youth";
}

Enjoy coding!!!

C# 7.0 – Throw expressions

C# 7.0 introduced throw expression, throwing exception has always been a statement and need to call a method to throw exception. Syntax is same as you’ve always used to throw exceptions difference is it can be also placed in the conditional expressions

With c# 7.0 exceptions can be thrown directly from within expression as shown below-

 public class ThrowExpressions
 {
 public static string ThrowExpressionMethod(string someString)
 {
 return string.IsNullOrWhiteSpace(someString) ? throw new ArgumentNullException("someString is empty") : someString;
 }
 }

 

 

 

 

C# 7.0 Numerical literals and OUT variables

Numeric literal syntax

Reading numbers makes it difficult to understand code. C# 7 has improvement done on the digit separator.

public static int IntegerLiterals()
 { return 1_000_000; }

public static double DoubleLiterals()
 { return 9_8_7.1_2_3; }

public static int BinaryLiterals()
 { return 0b0001_0000; }

 Console.WriteLine($" One Million : {NumericalLiterals.IntegerLiterals()}");
 Console.WriteLine($" Double : {NumericalLiterals.DoubleLiterals()}");
 Console.WriteLine($" Binary to Decimal : {NumericalLiterals.BinaryLiterals()}");

Output:

One Million : 1000000
Double : 987.123
Binary to Decimal : 16

 

Inline out variables

Earlier declaring out variables has to be done separately. Now this can be done in line as follows

public static int InlineOutVariables()
 {
 string input = "100";
 if (!int.TryParse(input, out int result))
 {
 return 0;
 }
 return result;
 }

You can declare out variable where you use it.  The declared out variables also leaks out of the if statement scope and used later.

 

 

mongodb as a service

Download and Install Mongo db for your OS from https://www.mongodb.com/download-center#community

Created a folder to store the collected data in mongodb e.g.:- C:Path\mongodb

Create a log folder in mongodb folder and create mongodb.log file.

Execute the below in command from the path where mongo is installed or set the global variables to execute this command.Update the path accordingly

mongod --dbpath "C:\Path\mongodb" --serviceName "LocalMongoDB" --logpath "C:\Path\mongodb\log\mongodb.log" --logappend --install

You should see in you serives (run services.msc) mongo service which will be in stopped state. Start the mongo db service.You may start monngodb from command prompt. For more details see this -https://docs.mongodb.com/manual/tutorial/manage-mongodb-processes/