Wednesday 30 July 2014

sharepoint check in and publish folder using Powershell


Add-PSSnapin "Microsoft.SharePoint.PowerShell"

### GLOBAL VARS, CHANGE HERE
$listname = "Style Library"
$url = "http://abcd:1234/sites/TestSite/"

function approveContent ($w, $listName) {
  $list = $w.Lists |? {$_.Title -eq $listName}
  foreach ($item in $list.Items)
  {
    if(($item -ne $null) -and ($item.LockId -ne $null)) {
      $item.ReleaseLock($item.LockId)
    }
    if( $item.File -ne $null) { $itemFile = $list.GetItemById($item.ID).File }
    else { $itemFile = $list.GetItemById($item.ID) }
   
    if( $itemFile.CheckOutStatus -ne "None" ) {
      $itemFile.CheckIn("Automatic CheckIn. (Administrator)")
      if( $item.File -ne $null) { $itemFile = $list.GetItemById($item.ID).File }
      else { $itemFile = $list.GetItemById($item.ID) }
    }
    if( $list.EnableVersioning -and $list.EnableMinorVersions) {
      $itemFile.Publish("Automatic Publish. (Administrator)")
      if( $item.File -ne $null) { $itemFile = $list.GetItemById($item.ID).File }
      else { $itemFile = $list.GetItemById($item.ID) }
    }
    if( $list.EnableModeration ) {
      $itemFile.Approve("Automatic Approve. (Administrator)")
    }
  }
}

$site = Get-SPSite $url
foreach ( $web in $site.AllWebs )
{
 approveContent $web $listname
}
Write-Output "OK"
Continue Reading...

datetime picker in sharepoint c#

Design Code:-

                      <SharePoint:DateTimeControl ID="dtpstartDt" DateOnly="true"  runat="server" IsRequiredField="true" CalendarImageUrl="/_layouts/images/calendar.gif"
                CssClassTextBox="itw-longDate"  LocaleId="7177" ToolTip="Example: 2000-12-31"
                DatePickerFrameUrl="/_layouts/iframe.aspx" />


Page Load:-

 dtpstartDt.DatePickerFrameUrl = SPContext.Current.Web.ServerRelativeUrl + "/_layouts/iframe.aspx";
 dtpstartDt.MinDate = DateTime.Now.AddDays(-1);
Continue Reading...

Programmatically Check Current user in SharePoint Group using C#

 public void CheckcurrentuserinGroup()
        {
            SPGroup grp = SPContext.Current.Web.SiteGroups["GroupName"];
            if (grp != null)
            {
                if (grp.ContainsCurrentUser)
                {
                    //True
                }
                else
                {
                    //false
                    SPUtility.HandleAccessDenied(new Exception("You don’t have access rights to see this content. Contact Administrator"));
                }
            }
        }
Continue Reading...

get user profile properties programmatically using c#

 public void getuserprofilesyncCustomattributesvalues(string MysitecollectionUrl)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite siteCollection = new SPSite(MysitecollectionUrl))
                {
                    using (SPWeb site = siteCollection.OpenWeb())
                    {
                        ServerContext context = ServerContext.GetContext(siteCollection);
                        UserProfileManager profileManager = new UserProfileManager(context);
                        foreach (UserProfile profile in profileManager)
                        {
                            string PersonalSiteUrl = ((string)profile["PersonalSpace"].Value);
                            string AccountName = ((string)profile["AccountName"].Value);
                        }
                    }
                }
            });

        }
 
  public void getuserprofilesyncDefaultattributesvalues(string MysitecollectionUrl)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(MysitecollectionUrl))
                {
                    ServerContext context = ServerContext.GetContext(site);
                    UserProfileManager profileManager = new UserProfileManager(context);
                    foreach (UserProfile profile in profileManager)
                    {
                        string Department = Convert.ToString(profile[PropertyConstants.Department].Value);
                        string AccountName = Convert.ToString((profile[PropertyConstants.AccountName].Value));
                        string Name = Convert.ToString((profile[PropertyConstants.PreferredName].Value));
                    }
                }
            });
        }
 
Continue Reading...

Programmatically Delete all Item in SharePoint List using c#

       
        public static void DeleteAllItems(string list)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite spSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb spWeb = spSite.OpenWeb())
                    {
                        spWeb.AllowUnsafeUpdates = true;
                        StringBuilder deletebuilder = BatchCommand(spWeb.Lists[list]);
                        spSite.RootWeb.ProcessBatchData(deletebuilder.ToString());
                        spWeb.AllowUnsafeUpdates = false;
                    }
                }
            });

        }
        private static StringBuilder BatchCommand(SPList spList)
        {
            StringBuilder deletebuilder = new StringBuilder();
            deletebuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
            string command = "<Method><SetList Scope=\"Request\">" + spList.ID +
                "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";

            foreach (SPListItem item in spList.Items)
            {
                deletebuilder.Append(string.Format(command, item.ID.ToString()));
            }
            deletebuilder.Append("</Batch>");
            return deletebuilder;
        }
   DeleteAllItems("Employee");
Continue Reading...

Set default master page and welcome page in sharepoint 2013 using Event receiver

 void ChangeMasterPage(SPWeb Web, string pstrMasterURL, string pstrCustomURL)
        {

            Web.MasterUrl = pstrMasterURL;

            Web.CustomMasterUrl = pstrCustomURL;

            Web.Update();

            Web.Dispose();

        }
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            SPWeb CurrentWeb = properties.Feature.Parent as SPWeb;
            CurrentWeb.MasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/seattle.master";
            CurrentWeb.CustomMasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/Custom.master";
            SPFolder _rootFolder = CurrentWeb.RootFolder;
            _rootFolder.WelcomePage = "Pages/Home.aspx";
            _rootFolder.Update();
            CurrentWeb.Update();
            foreach (SPWeb subweb in CurrentWeb.GetSubwebsForCurrentUser())
            {

                ChangeMasterPage(subweb, CurrentWeb.MasterUrl, CurrentWeb.CustomMasterUrl);

            }

            CurrentWeb.Dispose();
        }
        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb CurrentWeb = properties.Feature.Parent as SPWeb;

            CurrentWeb.MasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/seattle.master";

            CurrentWeb.CustomMasterUrl = CurrentWeb.Site.RootWeb.ServerRelativeUrl + "/_catalogs/masterpage/seattle.master";
            SPFolder _rootFolder = CurrentWeb.RootFolder;
            _rootFolder.WelcomePage = "Pages/Default.aspx";
            _rootFolder.Update();
            CurrentWeb.Update();
        }
Continue Reading...

Followers

Follow The Author