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...

Thursday, 13 March 2014

insert lookup field in sharepoint custom list using c#

 SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists[ListName];
                        SPListItem newitem = list.Items.Add();
                        newitem["Title"] = "New lookup";
                        SPFieldLookupValue newValue = new SPFieldLookupValue(1, "Title1");
                        newitem["Title1"] = newValue;
                        newitem.Update();
                    }
                }
            });
Continue Reading...

get all lookup value from sharepoint customlist using c#

 List<string> myCollection = new List<string>();
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList list = web.Lists[ListName];
                        int i = 0;
                        foreach (SPListItem item in list.Items)
                        {
                            SPListItem spitem = list.Items[i];
                            SPFieldLookupValue group = new SPFieldLookupValue(spitem[LookupColumnName].ToString());
                            string lookedUpItemTitle = group.LookupValue;
                            myCollection.Add(lookedUpItemTitle);
                            i++;
                        }
                    }
                }
            });
Continue Reading...

Saturday, 1 March 2014

Check the current Login user in sharepoint Group using c#

 
SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                  
                    SPWeb theSite = SPControl.GetContextWeb(Context);
                    SPUser theUser = theSite.CurrentUser;
                    string CurrentUserLoginName = theUser.LoginName;
                    string siteUrl = SPContext.Current.Web.Url;
                    using (SPSite site = new SPSite(siteUrl))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPGroupCollection groupCollection = web.SiteGroups;
                            foreach (SPGroup group in groupCollection)
                            {
                                SPUserCollection userCollection = group.Users;
                                string GroupName = group.LoginName;
                                if (GroupName == GrpName)
                                {
                                    foreach (SPUser user in userCollection)
                                    {
                                        string UserName = user.LoginName;
                                        if (UserName == CurrentUserLoginName)
                                        {
                                          
                                        }
                                    }
                                }
                            }

                        }
                    }
                });
Continue Reading...

Convert multiline text to string in sharepoint using c#

The below code is convert the SharePoint customList multiLine text column to string using c#

       String description = Convert.ToString(item["Description"]);

       SPHttpUtility.ConvertSimpleHtmlToText(description, -1);
Continue Reading...

Checkin-Publish folder in SharePoint using powershell


Add-PSSnapin "Microsoft.SharePoint.PowerShell"

### GLOBAL VARS, CHANGE HERE
$listname = "Style Library"
$url = "http://Abcd:1111/sites/SampleSite/"

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...

Followers

Follow The Author