0

I have a feature that creates a documnet library and then some views. One of the views is called my last modified documents.

When I activate it via site features it works. When i activate it via powershell it doesnt work and I know that the SPContext will be null when you execute it via powershell.

The question, is how can i create this view without using SPContext current user?

var oWeb = SPContext.Current.Web;
                                var currentUser = oWeb.CurrentUser.Name;

                                #region My last modified documents
                                web.AllowUnsafeUpdates = true;

                                var myLastModifiedDocumentsView =
                                    xxxDocumentLibrary.Views.Add(
                                        Constants.Views.xxxDocument.MyLastModifiedDocumentsName, //name
                                        new StringCollection(), //fieldsCollection
                                        "", //query
                                        10, //rowLimit
                                        true, //paged?
                                        false); //defaultview?
                                if (myLastModifiedDocumentsView != null)
                                {
                                    myLastModifiedDocumentsView.ViewFields.Add(Constants.DefaultFields.DocIconName);
                                    myLastModifiedDocumentsView.ViewFields.Add(Constants.DefaultFields.LinkFilenameName);
                                    myLastModifiedDocumentsView.ViewFields.Add(Constants.DefaultFields.TitleName);
                                    myLastModifiedDocumentsView.ViewFields.Add(Constants.ContentTypes.xxxDocument.Fields.PwCNextReviewDate);
                                    myLastModifiedDocumentsView.ViewFields.Add(Constants.DefaultFields.ModifiedName);
                                    myLastModifiedDocumentsView.ViewFields.Add(Constants.DefaultFields.ModifiedByName);
                                    myLastModifiedDocumentsView.ViewFields.Add(Constants.DefaultFields.CreatedName);
                                    myLastModifiedDocumentsView.ViewFields.Add(Constants.DefaultFields.CreatedByName);
                                    myLastModifiedDocumentsView.Query =
                                        String.Format(
                                            "<Where><Contains><FieldRef Name=\'{0}\' /><Value Type='User'>{1}</Value></Contains></Where><OrderBy><FieldRef Name=\'{2}\' Ascending=\'{3}\' /></OrderBy>",
                                            Constants.DefaultFields.ModifiedByName,
                                            currentUser,
                                            Constants.DefaultFields.ModifiedName,
                                            "FALSE");
                                    myLastModifiedDocumentsView.Update();
                                }
  • Are you receiving any error? Or just Powershell script ends with no errors? – Alvmad Sep 25 '13 at 13:06
  • SPContext.Current user is null, object refernce not set to an instance of an object! – Luis Valencia Sep 25 '13 at 13:11
1

You cannot use SPContext inside Feature receivers when using PS commands

Is very simple. cmd.exe process does not understand or know how to get your current context. If you are activating a feature via browser, you have the w3wp.exe process from which you can get the context.

If you need anything from the SPweb try to use this snippet var web = properties.feature.parent as SPWeb Properties is an instance of SPFeatureReceiverProperties

|improve this answer|||||
  • I know that long time ago. the question was how to do the SPView to create a view where modified=currentuser. – Luis Valencia Sep 25 '13 at 13:29
1

The answer is to use the [Me] instead of the currentuser in the view query. and then I dont need to use SP COntext anymore

|improve this answer|||||
  • Great news. I didn't know it. – Alvmad Sep 25 '13 at 13:36
  • 2
    Even in cases where CurrentUser is not null, your initial code would have hardcoded the user who activated the feature into the query (which is probably not what you want) – lgaud Sep 25 '13 at 13:38
  • good one!!!!!!!! – Luis Valencia Sep 25 '13 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.