Category Archives: Uncategorized

Show a waiting message -working on it- for a long running process

Thanks to Karsten:
http://www.ilikesharepoint.de/2013/04/sharepoint-create-a-wait-message-for-long-running-tasks/

Below code can be used for showing a working on it message in an application page’s button click :

function waitMessage()
{
window.parent.eval(“window.waitDialog = SP.UI.ModalDialog.showWaitScreenWithNoClose(‘This should not take long.’, ”, 90, 450);”);
}

Application Page1111

My Application Page111

///////////////////////////////////
using System;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Applications;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Diagnostics;

namespace LongRunningProject.Layouts.LongRunningProject
{
public partial class iRoomTest : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void btnSearch_Click(object sender, EventArgs e)
{

SPSite site = null;
string currsite = SPContext.Current.Web.Url.ToString();
try
{
using (site = new SPSite(currsite))
{
using (SPWeb web = site.AllWebs.Add(“iRoomTestingBlog”, “iRoomTestingBlog”, “This iRoomTesting1 is created by using c# code”, 1033, “BLOG#0”, true, false))
{
this.ClientScript.RegisterStartupScript(this.GetType(), “CloseWaitDialog”, @”

if (window.frameElement != null)
{
if (window.parent.waitDialog != null)
{
window.parent.waitDialog.close(); }
}
“);
Page.Response.Write(“New Website ” + web.Url);
//resConsole.WriteLine(“New Website {0}”,web.Url);
}
}
}
catch (Exception ex)
{
Response.Write(“Error —> ” + ex.ToString());
}
finally
{
site.Dispose();
}
}
}
}

Advertisements

setting incoming email settings of discussion forum list in sharepoint programmatically

hi,

 i was told to set the incoming email settings of  discussion forum list in sharepoint programmatically,so i was able to get this done with the below code:

It will take the  subsite’s name and add it with the email settings [alias] of our discussion forum list’s incoming email settings section:

private void GetListofDiscForumLists(SPWebCollection selNodewebCollec)
      {
          string restrictedFolderName = “Restricted”;
          string non_restrictedFolderName = “Non-Restricted”;

          try
          {
              foreach (SPWeb singleWeb in selNodewebCollec)
              {
                  if (singleWeb != null)
                  {
                      singleWeb.AllowUnsafeUpdates = true;

                      string singleweburl = singleWeb.Url;

                      using (SPSite objchildsite = new SPSite(singleWeb.Url))
                      {
                          using (SPWeb objchildweb = objchildsite.OpenWeb())
                          {
                              objchildweb.AllowUnsafeUpdates = true;
                              string objchildsweburl = objchildweb.Url;
                              if (objchildweb.Lists.Count > 0)
                              {
                                  for (int iListCount = objchildweb.Lists.Count – 1; iListCount > 0; iListCount–)
                                  {
                                      SPList singleDisclist = objchildweb.Lists[iListCount];

                                      if (singleDisclist.BaseTemplate == SPListTemplateType.DiscussionBoard)
                                      {
                                          SPList tempDiscForumList = singleDisclist;

                                          if (singleDisclist.CanReceiveEmail)
                                          {
                                              singleDisclist.EnableAssignToEmail = true;
                                              singleDisclist.EmailAlias = Regex.Replace(“Exp_”+objchildweb.Title, @”[^0-9a-z]”, “”, RegexOptions.IgnoreCase);
                                              SPFolder rootFolder = singleDisclist.RootFolder;
                                              rootFolder.Properties[“vti_emailusesecurity”] = 0;
                                              rootFolder.Properties[“vti_emailsaveattachments”] = 1;
                                              rootFolder.Properties[“vti_emailattachmentfolders”] = “subject”;
                                              rootFolder.Properties[“vti_emailoverwrite”] = 0;
                                              rootFolder.Properties[“vti_emailsavemeetings”] = 0;
                                              rootFolder.Properties[“vti_emailsaveoriginal”] = 1;
                                              rootFolder.Update();
                                              singleDisclist.Update();
                                          }
                                          else
                                          {
                                              throw new SPException(string.Format(“‘{0}’ can not receive mail”, singleDisclist));
                                          }
                                      }
                                  }
                              }
                              objchildweb.AllowUnsafeUpdates = false;
                          }
                      }
                      if (singleWeb.Webs.Count > 0)
                      {
                          GetListofDiscForumLists(singleWeb.Webs);
                      }
                  }

                  singleWeb.AllowUnsafeUpdates = false;
              }
          }
          catch (Exception expppo)
          {
              throw;
          }
     

hope this helps someone!

happy SharePointing!

Iterate through sub sites in a site collection

recently i was told to iterate through all the subsites and all the doc libs in a  site collection.

so, i had written the below code  in a  recursive way:

………………

getlistofwebsfromsitecollec(curresitecollecurl)

{

  try
     {
        SPSecurity.RunWithElevatedPrivileges(delegate()
          {
         using (SPSite _CurrentSelectedSite = new SPSite(strcurrentSelectedURLSiteNameList))
              {
                  using (SPWeb _CurrentSelectedWeb = _CurrentSelectedSite.OpenWeb())
                  {
                      _CurrentSelectedWeb.AllowUnsafeUpdates = true;
                      string mmcurrSelURL = string.Empty;
                
                  if (_CurrentSelectedWeb.Webs.Count > 0)
                   {
                      SPWebCollection childWebsofSiteCollec = _CurrentSelectedWeb.Webs;

                     GetListofWebs(childWebsofSiteCollec);

  }

 

 private void GetListofWebs(SPWebCollection childWebsofSiteCollect)
    {
        foreach (SPWeb singleSelectedWeb in childWebsofSiteCollect)
        {
            _mCurrSubWebTitleRetrieve = singleSelectedWeb.Title.ToString(); // (paramcurrSelectedNodeNameforSubSite)))
            _mCurrSubWebTitleURLRetrieve = singleSelectedWeb.Url.ToString();
            if (_mCurrSubWebTitleRetrieve != “Search”)
            {                                             

                try
                {
                    dttableGrpNamenAccessRights = RetrieveGroupNamesnApplyAccessRights(_mCurrSubWebTitleRetrieve, _mCurrSubWebTitleURLRetrieve);
                }
                catch (Exception expinRetrievenApplyPerms)
                {
                    LogthisError(expinRetrievenApplyPerms);
                }
                //NOW APPLY THE PERMISIONS
                try
                {
                   
                    dataTableGrpNamenAccessRightsContentType = RetrieveGroupNamesnApplyAccessRights(_mCurrSubWebTitleRetrieve, _mCurrSubWebTitleURLRetrieve);
                }
                catch (Exception expinRetrievenApplyPermsforCType)
                {
                    LogthisError(expinRetrievenApplyPermsforCType);
                }
            }
            try
            {
                GetListofWebs(singleSelectedWeb.Webs);
            }
            catch (Exception expinGetlistofwebs)
            {
                LogthisError(expinGetlistofwebs);
            }
        }
    }

}

}

 

Event receiver in SP 2013

hi,

below code explains how to apply permissions on an  item in a document library when an upload happens.using  a itemupdated event receiver in SP 2013 using VS 2012 :

1) it has removed all the role associations for the document . this includes entire group and  entire users.

2)it has also applied permissions on the  entire “everyone” users on the AD with  read permissions.

hope this helps someone.

using System;
using System.Web;
using System.Web.Configuration;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint.Administration;

namespace testt
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class tests1: SPItemEventReceiver
    {

        #region  variables

        #endregion  variables

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            SPUser currUser = properties.Web.CurrentUser;
            SPFieldUserValue flduserValue = new SPFieldUserValue(properties.Web, currUser.ID, currUser.Name);

            try
            {
                string currentuserIdd = properties.CurrentUserId.ToString();

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    SPSite CurrentSite = new SPSite(properties.SiteId);
                    SPWeb CurrentWeb = CurrentSite.OpenWeb(properties.RelativeWebUrl);
                    SPList CurrentList = CurrentWeb.Lists[properties.ListId];
                    SPListItem CurrentListItem;

                    SPWeb myRootWeb = CurrentSite.RootWeb;

                    CurrentListItem = CurrentWeb.Lists[properties.ListId].GetItemById(properties.ListItem.ID); 

                    string _mCurrentUserID = string.Empty;

                   
                   
                 //   SPGroup EnPUsersGroupinBIF = CurrentWeb.SiteGroups[“EnPUsers”];

                    if (CurrentListItem != null)
                    {
                        {
                            try
                            {

                                {
                                    //classification column a single line of text data type
                                    selectedClassificationValue = CurrentListItem[CLASSIFICATION].ToString();
                                }
                            }
                            catch (Exception explnc)
                            {
                                throw;
                            }
                            if (!string.IsNullOrEmpty(selectedClassificationValue) && selectedClassificationValue == “Confidential” && selectedClassificationValue != “–Select–“)
                            {
                                CurrentWeb.AllowUnsafeUpdates = true;

                                CurrentListItem.BreakRoleInheritance(false);

                                //Break the ITEM Inheritence and Remove the other default groups 2) get the taxonomy from the document library 3)                        //Break the Inheritence

 

              //SPUser mallusers = properties.Web.EnsureUser(“NT AUTHORITY\\authenticated users”); //all the enp users would be getting ReadOnly access to this document

                                                         

                                SPRoleAssignmentCollection SPRoleAssColn = CurrentListItem.RoleAssignments;
                                for (int i = SPRoleAssColn.Count – 1; i >= 0; i–)
                                {
                                    SPRoleAssignment roleAssignmentSingle = SPRoleAssColn[i];
                                    System.Type t = roleAssignmentSingle.Member.GetType();
                                    if (t.Name == “SPGroup” || t.Name == “SPUser”)
                                        SPRoleAssColn.Remove(i);
                                }                                                       

                                                        SPRoleAssignment mroleAssignmentforCurrUser = new SPRoleAssignment(currUser);
                                                        SPRoleDefinition mroleDefinitionforCurrUser = CurrentWeb.RoleDefinitions[“Contribute”];
                                                        mroleAssignmentforCurrUser.RoleDefinitionBindings.Add(mroleDefinitionforCurrUser);
                                                        CurrentListItem.RoleAssignments.Add(mroleAssignmentforCurrUser);

                                                        this.EventFiringEnabled = false;

                                                        //below code is working on 4th-june-2014 for modified by 4-june 9:30 pm
                                                        CurrentListItem[“Author”] = currentuserIdd;
                                                        CurrentListItem.Update();

                                                        CurrentListItem[“Editor”] = currentuserIdd;
                                                                                                                CurrentListItem.Update();
                                                        

                                                        this.EventFiringEnabled = true;
                                                        CurrentWeb.AllowUnsafeUpdates = false;
                            }
                                                        else if (!string.IsNullOrEmpty(selectedClassificationValue) && selectedClassificationValue != “Confidential” && selectedClassificationValue != “–Select–” && selectedClassificationValue == “Public”)
                            {
                                try
                                {
                                    CurrentWeb.AllowUnsafeUpdates = true;
                                    CurrentListItem.BreakRoleInheritance(false)                               
                                                     ///

                                    SPRoleAssignmentCollection SPRoleAssColnPub = CurrentListItem.RoleAssignments;
                                    for (int i = SPRoleAssColnPub.Count – 1; i >= 0; i–)
                                    {
                                        SPRoleAssignment roleAssignmentSingle = SPRoleAssColnPub[i];
                                        System.Type t = roleAssignmentSingle.Member.GetType();
                                        if (t.Name == “SPGroup” || t.Name == “SPUser”)
                                            SPRoleAssColnPub.Remove(i);
                                    }

                                    SPUser allusers = CurrentWeb.EnsureUser(“NT AUTHORITY\\authenticated users”);
                                    SPRoleAssignment AllBIFRoleassignment = new SPRoleAssignment(allusers);
                                    SPRoleDefinition biffroledefi = CurrentWeb.RoleDefinitions.GetByType(SPRoleType.Reader);
                                    AllBIFRoleassignment.RoleDefinitionBindings.Add(biffroledefi);
                                    CurrentListItem.RoleAssignments.Add(AllBIFRoleassignment);

 

                                    SPRoleAssignment mroleAssignmentforCurrUser = new SPRoleAssignment(currUser);
                                    SPRoleDefinition mroleDefinitionforCurrUser = CurrentWeb.RoleDefinitions[“Contribute”];
                                    mroleAssignmentforCurrUser.RoleDefinitionBindings.Add(mroleDefinitionforCurrUser);
                                    CurrentListItem.RoleAssignments.Add(mroleAssignmentforCurrUser);

                                    this.EventFiringEnabled = false;

                                    CurrentListItem[“Author”] = currentuserIdd; 

                                    CurrentListItem.Update();
                                   
                                    CurrentListItem[“Editor”] = currentuserIdd; 
                                    CurrentListItem.Update();
                                    this.EventFiringEnabled = true;
                                    CurrentWeb.AllowUnsafeUpdates = false;
                                }
                                catch (Exception expforPublicdocGroup) { LogthisError(expforPublicdocGroup); }
                            }
                                      }
                    }
                });
            }
            catch (Exception expforAccessConfid)
            {
                LogthisError(expforAccessConfid);
            }
        }

              public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
        }

        public void LogthisError(Exception Expfor)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                WebPartsLoggingService.LogError(“Malat”, Expfor.StackTrace);

            });
        }

        public object _mAccessRights { get; set; }
    }

 

}

 

create a custom menu ina lisviewbyquery web part in SP 2013

Yesterday client had asked us to create a  custom menu item in listviewbyquery web part. The  listviewbyquery web part was shown to the users with documents populated from multiple site collections and sub sites.

so  i was able to create with a custom menu item in the listviewbyquery web part so that when clicked on that a modal pop up [aspx ] /sp dialog  was populated.

<script language=”javascript”>
 
function Custom_AddDocLibMenuItems(m, ctx)
 
{
    

 //Add your Menu Name which you want to display
 
   var strDisplayText = “Add Reference”;

// parse the URL out of the itemTable
 
var URL = “”;
 
var index = itemTable.innerHTML.indexOf(“href=”);
 
if (index > 0)
 
{
 
  var str = itemTable.innerHTML.substr(index + 6);
 
  index = str.indexOf(‘”‘);
 
  if (index > 0)
 
  {
 
    URL = str.substr(0, index);

//  alert(URL);
 
 
  }
 
}
 
  var strAction = “javascript:OpenDialog(‘http://mysrvename:5182/_layouts/15/myLink/mylinkCustAction.aspx?docpath=&#8221; + URL + “‘)”;
   
  var strImagePath = “”;
 
 
 // Add our new menu item

  CAMOpt(m, strDisplayText, strAction, strImagePath);
 
  // add a separator to the menu
 
  CAMSep(m);
 
  // false means that the standard menu items should also be rendered
  // if you set true, it will show only the menu which you added( standard sharepoint menu items will not be available.
  // you can control by setting the value of true or false
  return false;

}
</script>

<script type=”text/javascript”> 
 function OpenDialog(myurl)
 {       
    var options = SP.UI.$create_DialogOptions();     
    options.url = myurl;   
      options.width = 250;  options.height = 250;  
     //options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback); 
       SP.UI.ModalDialog.showModalDialog(options); 
 }
</script>

provision a managed metadata column programmatically in SP 2010/ 2013

today i am going to talk about a sitaution where i  faced during my project :

the below post explains from Wictor, how to create/provision a MMS/taxonomy column using VS 2010 for SP 2010 env.

http://www.wictorwilen.se/Post/How-to-provision-SharePoint-2010-Managed-Metadata-columns.aspx

also there exists another blog which details about the same:

http://tjendarta.wordpress.com/2013/07/19/create-managed-metadata-column-taxonomyfieldtype-in-visual-studio/

hope this helps someone!