Tech Off Thread

5 posts

Moss 2007 - Activated Feature working only for Admin Account

Back to Forum: Tech Off
  • User profile image
    venkatzeus

    Hi,

     

    I had created a feature and activated in the SharePoint site using the Admin account. The feature was to count the number of files under each sub folder. The feature got activated successfully. I tested the code under the Admin account, and it was working.
    I tried to test with other users, who had design and contribute access. But when the other users checked, the count was not showing. No errors were generated. This is the code I have used.

     

     public static void GetOpenItems()
      {
    using (SPSite site = new SPSite(Constants.url))
      {
    using (SPWeb currentweb = site.OpenWeb())
      {
    SPListCollection mylists = currentweb.GetListsOfType(SPBaseType.DocumentLibrary);
    for (int q = mylists.Count - 1; q >= 0; q--)                           
    {
    SPList list = mylists[q];
    
    if (!list.Hidden)
    {                              
    currentweb.AllowUnsafeUpdates = true;
    SPFolder folder = list.RootFolder;
    SPView curview = list.DefaultView;
    SPViewFieldCollection fieldCol = curview.ViewFields;
    if (!fieldCol.Exists("FilesCount"))
    {
    AddField("FilesCount", SPFieldType.Text, list.Fields);
    fieldCol.View.DefaultView = true;                                      
    curview.ViewFields.Add("FilesCount");
    curview.Update();                                  
    }
                                               
    int jouas = 0;                              
    if (folder != null && folder.Name.CompareTo("Forms") != 0)
    {
       foreach (SPFolder subFolder in folder.SubFolders)
      {                                         
    if (subFolder.Name.CompareTo("Forms") != 0)
        {
        jouas = GetItemsForFolderInt(subFolder);
       subFolder.Item["FilesCount"] = jouas;
       subFolder.Item.Update();    
    }
    }
    }                              
      }
    }               
    }
    }
    }

     

    These are the steps I used to install the feature from dev environment to Production environment.

     

    1) Copied the dll from GAC and pasted in Production server's GAC.

     

    2) Copied the relevant feature folders from 12 hive and pasted in the corresponding 12 hive of production server

     

    3) Used the Stsadm Install and Activate commands for the site.

     

    Is there anything else I should be doing?

     

    Please help.. I am not sure, what to do. The feature is working only for Admin account and not for users having other permissions.

     

    Thank you.

  • User profile image
    fabian

    @venkatzeus: Thats is NOT the recommended way of deploying a MOSS Feature. You need to create a WSP packages and deploy it using stsadm deploy/install/active feature command

    Download WSPBuilder from Code Plex. It integrates nicely into Visual Studio and makes it easy to create an WSP packaged. (If you are using sharepoint 2010 and vs.net 2010 similar tools are buildt into vs.net)

    In ordet to solve you other problem, take a look here:

    http://blogs.syrinx.com/blogs/sharepoint/archive/2008/08/11/running-code-inside-moss-with-elevated-permissions.aspx

    Hope this helps

  • User profile image
    venkatzeus

    Hi,

    Thank you very much for the reply. I understand that I should have used WSPBuilder. I would download it and start working with it as you mentioned.

    I checked the blog and changed code to below:

        public static class SpSecurityHelper
        {
            public static SPSite GetElevatedSite(SPSite theSite)
            {
                SPUserToken sysToken = GetSystemToken(theSite);
                return new SPSite(theSite.ID, sysToken);
            }
    
            public static SPUserToken GetSystemToken(SPSite site)
            {
                site.CatchAccessDeniedException = false;
                try
                {
                    return site.SystemAccount.UserToken;
                }
                catch (UnauthorizedAccessException)
                {               
                    SPUserToken sysToken = null;
                
                    SPSecurity.RunWithElevatedPrivileges(
                    delegate()
                    {
                        using (SPSite lolcatKiller = new SPSite(site.ID))
                        {
                            sysToken = lolcatKiller.SystemAccount.UserToken;                    
                        }
                    }
                    );
                    return sysToken;
                }
            }
        } 
    
    
      public class ItemCounting : SPItemEventReceiver
        {
            public override void ItemAdded(SPItemEventProperties properties)
            {
                base.ItemAdded(properties);
                GetOpenItems();
            }
    
    public override void ItemDeleted(SPItemEventProperties properties)
            {
                base.ItemDeleted(properties);
                GetOpenItems();
            }
    
    
    public static void GetOpenItems()
      {
    using (SPSite site = SpSecurityHelper.GetElevatedSite(new SPSite(Constants.url)))
      {
    using (SPWeb currentweb = site.OpenWeb())
      {
    SPListCollection mylists = currentweb.GetListsOfType(SPBaseType.DocumentLibrary);
    for (int q = mylists.Count - 1; q >= 0; q--)                           
    {
    SPList list = mylists[q];
    
    if (!list.Hidden)
    {                              
    currentweb.AllowUnsafeUpdates = true;
    SPFolder folder = list.RootFolder;
    SPView curview = list.DefaultView;
    SPViewFieldCollection fieldCol = curview.ViewFields;
    if (!fieldCol.Exists("FilesCount"))
    {
    AddField("FilesCount", SPFieldType.Text, list.Fields);
    fieldCol.View.DefaultView = true;                                      
    curview.ViewFields.Add("FilesCount");
    curview.Update();                                  
    }
                                               
    int jouas = 0;                              
    if (folder != null && folder.Name.CompareTo("Forms") != 0)
    {
       foreach (SPFolder subFolder in folder.SubFolders)
      {                                         
    if (subFolder.Name.CompareTo("Forms") != 0)
        {
        jouas = GetItemsForFolderInt(subFolder);
       subFolder.Item["FilesCount"] = jouas;
       subFolder.Item.Update();    
    }
    }
    }                              
      }
    }               
    }
    }
    }
    }

     

    The code is still running only for Admin account and not for other users. Any suggestions on this.

    Thank you

  • User profile image
    Lee_Dale

    Probably because the list item is being checked out using the Admin account and not published so the Admin user will be the only one who can see the changes.

  • User profile image
    venkatzeus

    Hi,

    Thank you very much the reply.

    Is there any solution you could suggest to correct that behavior.

    Thank you

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.