Wednesday, September 23, 2015

The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.

We may get "The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again." issue when try to BreakRoleInheritance  of list programmatically. To resolve this, Add AllowUnsafeUpdates = true.

For Example,

web.AllowUnsafeUpdates = true;

Make it false once BreakRoleInheritance is over.


Monday, September 21, 2015

Export all events to iCal format programmatically


We all know about iCal format which is used to import the Calendar items to Outlook. We can able to export a single item to iCal using OOTB. But we doesn't have the option to get all the events in single .ics file. Here i am going to provide a code base to export all the events as .ics format

 StringBuilder sw = new StringBuilder();
                sw.AppendLine("BEGIN:VCALENDAR");
                sw.AppendLine("VERSION:2.0");
                sw.AppendLine("METHOD:PUBLISH");
                foreach (var item in items)
                {

                    sw.AppendLine("BEGIN:VEVENT");
                    sw.AppendLine("CLASS:PUBLIC");
                    sw.AppendLine(string.Format("CREATED:{0:yyyyMMddTHHmmss}", DateTime.UtcNow));
                    sw.AppendLine("DESCRIPTION: " + Convert.Tostring(item["Description"]));
                    sw.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmss}", Convert.ToDateTime(item[EndDate]));
                    sw.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmss}", Convert.ToDateTime(item[StartDate]));
                    sw.AppendLine("SEQUENCE:0");
                    sw.AppendLine("UID:" + Guid.NewGuid().ToString());
                    sw.AppendLine("LOCATION:" + Convert.ToString(item["Location"]));
                    sw.AppendLine("SUMMARY;LANGUAGE=en-us:" + Convert.ToString(item["Title"]));
                    sw.AppendLine("END:VEVENT");
                }
                sw.AppendLine("END:VCALENDAR");

                Page.Response.Clear();
                Page.Response.ContentType = "text/calendar";
                Page.Response.AddHeader("content-disposition", "inline; filename=" + "Test.ics");
                Page.Response.Write(sw.ToString());
                Page.Response.Flush();

Put the above code base in your webpart. Then iCal format will work like an awesome.

Thursday, September 17, 2015

Unable to cast object of type to'Microsoft.SharePoint.WebPartPages.WebPart'

Some of us face this issue while casting webparts in the page. Your code is like this when you get this error.

foreach (Microsoft.SharePoint.WebPartPages.WebPart item in wpManager.WebParts)
            {
                if (item.Title.Equals(webpartTitle))
                {
                    isExists = true;
                }

            }

Make like this

foreach (System.Web.UI.WebControls.WebParts.WebPart item in wpManager.WebParts)
            {
                if (item.Title.Equals(webpartTitle))
                {
                    isExists = true;
                }

            }

Monday, September 14, 2015

Export Taxonomy Terms to SharePoint List


SharePoint give an option to have store metadata information. That's called Taxonomy. It's common place to have terms. Now i am going to give a script to export this terms to list.

Script

$site = Read-Host "Enter  Site URL :"
$web = Read-Host "Enter Sub Site URL :"
$spweb= Get-SPWeb $web
$country = $spweb.Lists["Your List Name"]

$termSet = Get-SPTaxonomySession -Site $site
$termStore = $termSet.TermStores[0]
$termGroup = $termStore.Groups[“TermGroupName”]
$termStore = $termStore.Name
foreach ($termGroups in $termGroup.TermSets)
{
    foreach ($termSets in $termGroups.Terms)
    {
        $newItem = $country.items.add()
        $newItem["Title"] = $termSets.Name
        $newItem.Update()
        Write-Host $termSets.Name " has been added !!!" -ForegroundColor Green
    }
}

That's all. Terms will be added in the list

Tuesday, September 8, 2015

Updating Calendar Overlay using Powershell

Introduction

SharePoint having an option for Calendar overlay. Click here to have introduction. Now, I am going to share a script to update it.

Calendar Settings

Once we applied the Calendar Overlay to the list. The Calendar Settings get append with AggregationCalendar. 


We need to modify this Aggregation Calendar for changes we want in Calendar Overlay. This Calendar Settings is a string. So we can do replace string with old string using script. For example, If we want to change the color. Script will be like this,

$web = Read-Host "Enter Site URL :"
$spweb= Get-SPWeb $web
$calendar = $spweb.Lists["Your List Name"]
$calendar.DefaultView.CalendarSettings = $calendar.DefaultView.CalendarSettings.Replace("Color=""3""","Color=""2""")
$calendar.DefaultView.Update()

That's all Calendar Settings has been updated

Wednesday, September 2, 2015

Select lookup field in NewForm.aspx using Querystring


We all knew about lookup column in SharePoint. It is used to get reference from another list. Here, I am going to give an idea to select this lookup column in New form using querystring.

Pass your querystring to Newform.aspx like

http://yourmachinename:portnumber/lists/listname/newform.aspx?itemId=1

Insert a content editor in list's new form. Paste the below script in content editor
<script>
$(document).ready(function ($) {
function SetFormFields() {
    
    var QItemID = GetQueryStringParameter("itemId", "NULL");
    var IDLookup = SPUtility.GetSPFieldByInternalName('yourcolumnname');
    if (QItemID != "NULL") {
        IDLookup.SetValue(QItemID);
    }    
}
});

function GetQueryStringParameter(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}
</script>