Blog Archives

// Build 2016 – The Future of C# (CH9)

Since Roslyn shipped last year we’ve been hard at work building on it to improve tooling, add new language features and enhance Roslyn itself. In this session Dustin and Mads will play with fire, showing early versions of new C# 7 language features such as tuples and pattern matching, new editor features like code style and source generators, and much more.

The news about 7.0 version are:

  • Binary Literals
  • Local Functions
  • Tuple
  • Pattern Matching
  • Ref Locals and Ref Returns
  • Records

Take a look at the presentation

 

Thanks

SEO – Change Meta Tags Dynamically in SharePoint or Asp.net – (JavaScript & C#) – Google Crawlers

Google-Crawl-Index-550x331

Meta tags are a great way for webmasters to provide search engines with information about their sites. They can be used to provide information to all sorts of clients, and each system processes.

Meta tags are added to the <head> section of your HTML page and you can add them dynamically use JavaScript when page load or using c#:

The following examples explain how can you add it on your header Page:

1. Use c#

public static void ApplyTagsCSharp(string xtitle, string xkeywords, 
string xdescription, Page page)
        {
            page.Title = xtitle;
            ContentPlaceHolder contentPlaceHolder = 
           (ContentPlaceHolder)page.Master.FindControl("PlaceHolderPageTitle");
            contentPlaceHolder.SetRenderMethodDelegate
            (delegate(System.Web.UI.HtmlTextWriter writer,
            System.Web.UI.Control Container)
            {
                writer.Write(xtitle);
            });

            ////Add Keywords Meta Tag
            HtmlMeta keywords = new HtmlMeta();
            keywords.HttpEquiv = "keywords";
            keywords.Name = "keywords";
            keywords.Content = xkeywords;
            page.Header.Controls.Add(keywords);

            //Add Description Meta Tag
            HtmlMeta description = new HtmlMeta();
            description.HttpEquiv = "description";
            description.Name = "description";
            description.Content = xdescription;
            page.Header.Controls.Add(description);
        }

2. Use JavaScript with ClientScriptManager

public static void ApplyTagsJS(string title, string keywords, 
string description, string author, ClientScriptManager csx, System.Type tp)
        {
            String csname2 = "isTagFunction";
            Type cstype2 = tp;

            if (!csx.IsStartupScriptRegistered(cstype2, csname2))
            {
                StringBuilder cstext2 = new StringBuilder();
                cstext2.Append("");
                if (!string.IsNullOrEmpty(keywords))
                {
                    cstext2.Append("$('meta[name=keywords]').remove();");
                    cstext2.AppendFormat("$('head').append( '');", keywords);
                }
                if (!string.IsNullOrEmpty(description))
                {
                    cstext2.Append("$('meta[name=description]').remove();");
                    cstext2.AppendFormat("$('head').append( '' );", description);
                }
                if (!string.IsNullOrEmpty(title))
                {
                  cstext2.AppendFormat("document.title = \"{0}\"; ", title);
                }
                csx.RegisterStartupScript(cstype2, csname2, cstext2.ToString());
            }
        }

Thanks

New ASP.Net Core 1.0

In 2002… was born ASP.NET … after 15 years we are presenting a new framework ASP.NET 5 and replace ASP.NET 4.6…

ASP.NET 5 is now ASP.NET Core 1.0.

.NET 5 is now .NET Core 1.0.

Entity Framework 7 is now Entity Framework Core 1.0 or EF Core 1.0

.NET Core introduce a new concept multi-platform and open source as you can see on the following image:

net core

for more info about .net core check the following links:

https://github.com/dotnet/core

http://docs.asp.net/en/latest/getting-started/installing-on-linux.html

https://blogs.msdn.microsoft.com/dotnet/2014/12/04/introducing-net-core/

Thanks

Build and Send email method to SharePoint – Using Outgoing SMTP Configuration on Central Administration.

To create and Send emails on sharepoint programmatically you can use the following code, but first you need configure the following things:

  • Configure SMTP on server.
  • Configure Outgoing E-Mail on Central Admininstration

(Central Administration > System Settings > Configure outgoing e-mail settings)

  • Oubound SMTP server is server’s IP.

Code:

StringBuilder body = new StringBuilder();
body.Append("TEXT OF BODY EMAIL"); // TextBox
string subjectEmail = "SUBJECT OF EMAIL"; // TextBox
string senderTo = "fcsharepoint@outlook.com"; //TextBox

SendEmail(senderTo, subjectEmail, body);

Method:

 public bool SendEmail(string sTo, string sSubject, StringBuilder sBody)
 {
  // Email header
  StringBuilder sHeader = new StringBuilder();
  sHeader.Append("<html>");
  sHeader.Append("<body>");

  // Email footer
  StringBuilder sFooter = new StringBuilder();
  sFooter.Append("</body>");
  sFooter.Append("</html>");

  string htmlBody = sHeader.ToString() + sBody.ToString() + sFooter.ToString();

   //Get the Sharepoint SMTP information from the SPAdministrationWebApplication
  string smtpServer = 
  SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
   
  string smtpFrom = 
  SPAdministrationWebApplication.Local.OutboundMailSenderAddress;

 // Add the alternate views instead of using MailMessage.Body
  MailMessage m = new MailMessage();
  m.From = new MailAddress(smtpFrom);
  m.To.Add(new MailAddress(sTo));
  m.Subject = sSubject.ToString();
  m.Body = htmlBody;
  m.IsBodyHtml = true;

 SmtpClient client = new SmtpClient(smtpServer);

 try
 {
   client.Send(m);
   return true;
 }
  catch (Exception) { }
  return false;
}

Check if Current User is member of SharePoint Group’s – Programmatically c#

In SharePoint, we have the 3 predetermined permission groups:

  • Visitors
  • Members
  • Owners

You can see this permissions grpups in the /_layouts/permsetup.aspx page.

(Site settings->People and Groups->Settings->Setup groups)

There are properties on the SPWeb class:

  • SPWeb.AssociatedVisitorGroup
  • SPWeb.AssociatedMemberGroup
  • SPWeb.AssociatedOwnerGroup

Programmatically you can check if current user are in one of those Groups, using the following code:

bool isVisitor = false;
bool isMember = false;
bool isOwner = false;

SPWeb web = SPControl.GetContextWeb(System.Web.HttpContext.Current);
            
SPContext currentContext = SPContext.Current;
                  
if (currentContext != null && currentContext.Web.CurrentUser != null)
{
   SPGroupCollection webGroups = web.CurrentUser.Groups;
   if (webGroups != null && webGroups.Count > 0)
   {
       foreach (SPGroup group in web.Groups)
       {
         if (group.ToString() == web.AssociatedVisitorGroup.ToString())
         {
            isVisitor = web.IsCurrentUserMemberOfGroup(group.ID);
         }
         if (group.ToString() == web.AssociatedMemberGroup.ToString())
         {
            isMember = web.IsCurrentUserMemberOfGroup(group.ID);
         }
          if (group.ToString() == web.AssociatedOwnerGroup.ToString())
         {
            isOwner = web.IsCurrentUserMemberOfGroup(group.ID);
         }
       }
   }
if(isVisitor == true)
{
 // User is Visitor
}
if(isMember == true)
{
  // User is Member
}
if(isOwner == true)
{
  // User is Owner
}

Hide Ribbon and SuitBar of SharePoint 2013 for a specifique Zone – Using JavaScript and C#

One way to hide Ribbon and SuitBar of SharePoint 2013 for a specific zone, like Internet for example – you can use Javascript + Css and run it in a UserControl.

1 – Create a simple UserControl on your solution and add it on your MasterPage.

In ASCX file of your UserControl add the following code:

<script type="text/javascript">
    function hideRibbon(){
        document.getElementById("s4-ribbonrow").style.display = "none !important";
        document.getElementById("suiteBar").style.display = "none !important";
    }
</script>

On code behind of UserControl,  you go execute the javascript function, using the following code:

Example to hide in Internet zone

using Microsoft.SharePoint.Administration;
...

protected void Page_Load(object sender, EventArgs e)
{
 if (SPContext.Current.Site.Zone == SPUrlZone.Internet)
 {
 Page.ClientScript.RegisterStartupScript(this.GetType(),"","hideRibbon();",true);
 }
}

Custom menu in SharePoint using Taxonomy Term Store Navigation – Programmatically C#

This is a simple example how to access to Taxonomy Term Store properties in SharePoint using c# and create a Custom Menu.

First, create the terms on your Taxonomy Manage Navigation. – Site settings> Site Administration > Term Store Management

To get the Simple Link URL information using C#:

DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Title"), 
                                       new DataColumn("Url") });
     ...
         foreach (var term in termSet.Terms)
         {
            if (term.IsDeprecated == false)
            {
               try
               {
                 var name = term.Name;
                 var url = term.LocalCustomProperties["_Sys_Nav_SimpleLinkUrl"];
                 dt.Rows.Add(new String[] { name, url });                 
               }
               catch (KeyNotFoundException) { }
             }
           }
       ...
         rptFooter.DataSource = dt; 
         rptFooter.DataBind();

Create ASPX repeater to construct your Menu

<asp:Repeater ID="rptFooter" runat="server">
 <ItemTemplate>
  <li>
  <asp:Button runat="Server" Text='<%# DataBinder.Eval (Container.DataItem, "Title") %>'
  PostBackUrl='<%# DataBinder.Eval (Container.DataItem, "Url") %>' />
 </li>
  </ItemTemplate>
</asp:Repeater>

The full list of Term Set properties on Manage Navigation in SharePoint 2013:

_Sys_Nav_Title: the Navigation Node Title

_Sys_Nav_FriendlyUrlSegment: the Friendly Url, also represented by the FriendlyUrlSegment property of the NavigationTerm class.

_Sys_Nav_TargetUrl: the target URL, also represented by the TargetUrl property of the NavigationTerm class.

_Sys_Nav_TargetUrlForChildTerms: the target URL for child terms, also represented by the TargetUrlForChildTerms property of the NavigationTerm class.

_Sys_Nav_CatalogTargetUrl: the catalog target URL, also represented by the CatalogTargetUrl property of the NavigationTerm class.

_Sys_Nav_CatalogTargetUrlForChildTerms: the catalog target URL for child terms, also represented by the CatalogTargetUrlForChildTerms property of the NavigationTerm class.