About Me

My photo
a Dynamic and Energetic guy.....

Wednesday, June 18, 2014

Add A WebPart to SharePoint Page Programatically Throws File CheckOut exception

When we add a WebPart to a page, programatically we have to checkout the page.

Even we try to Checkout the file using C# code, it throws the exception.
We have to go to the Document library that resides the required page.
Then go to the Library settings section.


Then go to the Versioning settings Section.
We have to set NO to "Require Checkout" option.

Then Our Webpart will be deployed to the page successfully.





Friday, May 9, 2014

Execution of PowerShell Script is disabled on the system

When we RUN PowerShell Scripts, we are getting "PowerShell Script is disabled on the system"

This is mainly because of EXECUTION POLICY.


We can run the command and get the CURRENT security status. It is "Restricted"

So we have to increase security for Scripts.

No, we can't increase Security if we are not running the Command Prompt with elevated privileges.
Yes, Now the Security level is upgraded.

Now we can run POWERSHELL Scripts.





Thursday, May 8, 2014

Delete files using PowerShell based on Created Time

We have to clean our SharePoint backup location after 7 days.
We have decided to create a POWERSHELL script and run it as a WINDOWS TIMER.
It is simple.

The PowerShell Script,

# Change the value $oldTime in order to set a limit for files to be deleted.
$oldTime = [int]1 # 30 days
foreach ($path in Get-Content "pathList.txt") {
# Write information of what it is about to do
Write-Host "Trying to delete files older than $oldTime days, in the folder $path" -ForegroundColor Green
# deleting the old files
Get-ChildItem $path -Recurse -Include "*.PNG", "*.bak" | WHERE {($_.LastWriteTime  -le $(Get-Date).AddDays(-$oldTime))} | Remove-Item -Force
}



there we have set 3 parameters.
Filepath   = the folder that we needs to clean
DAYS    = deleting files older than this DAYS
Types     = the file types that we need to delete

the "pathList.txt" has all the File paths that we have to delete.

 


We can delete SELECTED file types.


After we run the script, we can see all the FILES TYPES (.png) been deleted if OLDER than specify DAYS.

We can set a windows timer service and execute the .ps1 file



Tuesday, May 6, 2014

Get Data from a Forum / Discussion Board in SharePoint

======================FORUM, TITLE column==============

SPQuery query = new SPQuery();
query.Query = string.Concat(
    "<Where>",
        "<Eq>",
            "<FieldRef Name='IsRootPost'/>",
            "<Value Type='Number'>1</Value>",
        "</Eq>",
    "</Where>",
    "<OrderBy>",
        "<FieldRef Name='DiscussionTitle' Ascending='TRUE' />",
    "</OrderBy>");
query.ViewFields = "<FieldRef Name='DiscussionTitle' />";
query.ViewFieldsOnly = true;

SPListItemCollection _items = [MyList].GetItems(query);'

Sunday, April 27, 2014

Add a FBA User to a SharePoint Group Programatically

SPSecurity.RunWithElevatedPrivileges(delegate
                               {
                                   using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                                   {
                                       using (SPWeb web = site.OpenWeb())
                                       {
                                           //adding user to the contributor group
                                           web.AllowUnsafeUpdates = true;
                                           //i:0#.f|sql-membershipprovider|chana

                                           string usernameWithProvider = "i:0#.f|sql-membershipprovider|" + usr.UserName;
                                          
                                           web.AllUsers.Add(usernameWithProvider, txtUserName.Text, txtUserName.Text, "");
                                           try
                                           {
                                               SPUser spuser = web.AllUsers[usernameWithProvider];
                                               if (spuser != null)
                                               {
                                                   SPGroup spGroup = web.Groups["Dev Members"];

                                                   if (spGroup != null)
                                                       spGroup.AddUser(spuser);
                                                   web.Update();
                                                   web.AllowUnsafeUpdates = false;
                                               }
                                           }
                                           catch { }
                                           web.Update();
                                           web.AllowUnsafeUpdates = false;
                                       }
                                   }

                               });

Monday, March 17, 2014

SharePoint User Token Code

try
            {
                SPSite tempSite = new SPSite(siteURL);
                string loginNameInFull = @"i:0#.w|" + username;
                SPUserToken userToken = tempSite.OpenWeb().AllUsers[loginNameInFull].UserToken;
                using (SPSite site = new SPSite(siteURL, userToken))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList olist = web.Lists[listName];
                        SPListItemCollection ocol = olist.Items;
                        return ocol;
                    }
                }
            }

This is passing credentials to Windows Authentication
UserName append with "i:0#w|" 


Sunday, February 16, 2014

Convert EXCEL file to Project Plan

When we have data in an EXCEL with number of dates, we can convert that data to Project Plan without any other software.




Got the excel magic from


Tuesday, December 10, 2013

Schedule Task for SharePoint Site Backup

1) Open notepad
2) Create a command using STSADM
3) Set the path to store BACKUP
4) Go to TASK SCHEDULEr in server
5) Set the Schedule and account with elevated priviledges

6) Verify the backup and the location

COMMAND

@echo off
echo ===============================================================
echo Back up the farm to E:\CSKH_Backups
echo ===============================================================
cd %COMMONPROGRAMFILES%\Microsoft Shared\web server extensions\15\BIN
@echo off
stsadm.exe -o backup -url http://myportal -filename \\NASFSVS\KMSadmin_share\portalSP%date:~-2%%date:~4,2%%date:~7,2%.bak -overwrite -nositelock
echo completed

*** One Important Thing when creating TASK

How the file name set:
portalSP%date:~-2%%date:~3,2%%date:~0,2%.bak
if date is 29 - 09  - 15 ==> portalSP150929.bak
               01 2 34 5 67


My Masters