Jun 1, 2017

Access Webpart maintenance page in sharepoint2013

If you guys wanted to see what are the webparts available on your sharepoint page then just try this simple trick

Just append ?Contents=1 to the end of the URL for the page.

Simple right! :)
Happy coding

Get images from SharePoint Image library and attach them to email message using CSOM

Hello everybody,

Today lets try getting images from a Sharepoint image library and attach them to Mail message using CSOM.

My below code does the job. 

 public static MailMessage GetConfigurationImages(ClientContext context, System.Net.Mail.Attachment MailAttachment, Microsoft.SharePoint.Client.File file, MailMessage message, String imageName)
        {

            Logger.Info("Getting Configuration Images");
            var docs = context.Web.Lists.GetByTitle("ConfigurationImages");
            var qry = CamlQuery.CreateAllItemsQuery();

            var srcItems = docs.GetItems(qry);
            context.Load(srcItems, icol => icol.Include(i => i.FileSystemObjectType, i => i["FileRef"], i => i.File));
            context.Load(docs, l => l.RootFolder);
            context.ExecuteQuery();
            foreach (var item in srcItems)
            {
                file = item.File;
                var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl);
                var fileStream = fileInfo.Stream;
                MailAttachment = new System.Net.Mail.Attachment(fileStream, MediaTypeNames.Application.Octet);
                MailAttachment.Name = file.Name;
                MailAttachment.ContentId = file.Name;
                if (file.Name != imageName)
                { message.Attachments.Add(MailAttachment); }

            }
            return message;

Thanks. 

Get values from Sharepoint List using CSOM

Hey Fellas,

Well there are many times we need to call the SharePoint list for data in our CSOM applications,
Here below i have a Sharepoint List named "Settings" which contains the desired data.
I am using CSOM to get that data and returning it on the call of my function.

public static String GetSettings(ClientContext context, String Key)
        {

            List settingsList = context.Web.Lists.GetByTitle("Settings");
            String value = null;
            var query = new CamlQuery() { ViewXml = "" + Key + "" };
            ListItemCollection items = settingsList.GetItems(query);
            context.Load(items);
            context.ExecuteQuery();

            try
            {
                foreach (ListItem listItem in items)
                {
                    value = listItem["Value"].ToString();

                }
               
            }
            catch (Exception ex)
            {
                Logger.Info(" ");
               
            }
            return value;

        }

Let me know if this works for you.
Thanks people!

Send Email using CSOM

Here we are trying to send mails using CSOM. We make use of SMTP Client.
Below is the lines of code that worked for me to send mails

public static void SendEmail(ClientContext context, EmailTemplate emailDetailsObj, MailMessage message)
        {
            Logger.Info("Sending Mail");
            try
            {
                SmtpClient client = new SmtpClient();
                String fromEmail = ConfigurationManager.AppSettings["FromEmail"];
                client.Host = ConfigurationManager.AppSettings["MailHost"];
                message.IsBodyHtml = true;
                message.Subject = emailDetailsObj.Subject;
                message.Body = emailDetailsObj.Body;
                message.From = new MailAddress(fromEmail);
                FillToAndBcc(context, message, emailDetailsObj.To);

                client.Send(message);
               
                Console.WriteLine("Sending Mails..");
                Logger.Info("Mail sent successfully");
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
               
            }
  }

Here i have my values in my app.config file and I am getting them using Configuration manager

Correct way to use ConfigurationManager.AppSettings Property in c#

In this blog post I will take you through my simple code where I had to get my "SiteUrl" value present in app.config file.

We will be using ConfigurationManager.AppSettings property to get our value.

Make sure your app.config file in your Console application is having below value
        



Now in our program.cs lets get this value in the below manner

string siteUrl = ConfigurationManager.AppSettings["SiteUrl"];

Make sure you have proper references. We need to add a reference to using System.Configuration;

Hope it helps!
Thanks fellas.

Logging using Log4net in C# Console Application

This post lets get some insights on how to log errors using Log4net in our console application.
Lets get started by creating a console application in Visual studio.
Lets now configure log4net in our console application. To do this lets follow below steps.
1) Go to references and Manage nugget packages.



 2)Browse for Apache log4net and install the package. After this package is installed you will see a lognet dll added in your references.



3)We now need to add below lines of code to our app.config file in the ConfigSections tag.


4) In Program.cs lets try some code 

private static readonly ILog Logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

static void Main(string[] args)
        {
            Logger.InfoFormat("Running as {0}", WindowsIdentity.GetCurrent().Name);
            try
            {
//your code here
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
             }
         
        }
     
We can also use 
Logger.Debug(),
Logger.Warn() etc.
     
    5) When the above code is run a log file is created in the location you specified in app.config.
in our case it is
       c:\\Logfile.txt
     
     Please try it and let me know how it worked for you.
Thanks all.
      

Mar 4, 2014

To get Site Quota in sharepoint using c#

Hi ..

 To get sharepoint site quota using c# I have done the below code and it works perfectly fine.
 Mine is a console app:

 use these:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

And add the below reference from this path:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI
 Microsoft.sharepoint.dll

 program.cs code snippet:
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace ConsoleApplication1
 {
  class Program
  {
     static void Main(string[] args)
     {
            SPWebService contentService = SPWebService.ContentService;
foreach (SPQuotaTemplate quotaTemplate in contentService.QuotaTemplates)
{
            long StorageMaximumLevel = (quotaTemplate.StorageMaximumLevel) / (1024 * 1024);
long StorageWarningLevel = (quotaTemplate.StorageWarningLevel) / (1024 * 1024);
double UserCodeMaximumLevel = (quotaTemplate.UserCodeMaximumLevel);
double UserCodeWarningLevel = (quotaTemplate.UserCodeWarningLevel);
Console.WriteLine("Name: {0},QuotaID: {1},StorageMaximumLevel: {2}MB,StorageWarningLevel: {3}MB,UserCodeMaximumLevel: {4}MB, UserCodeWarningLevel: {5}MB ", quotaTemplate.Name, quotaTemplate.QuotaID, StorageMaximumLevel, StorageWarningLevel, UserCodeMaximumLevel, UserCodeWarningLevel);
}
Console.ReadLine();
}
}
}